C++ 引用std::cout会导致SEGFULT
以下代码导致gcc(Debian 6.3.0-18+deb9u1)6.3.0 20170516上出现SEGFULTC++ 引用std::cout会导致SEGFULT,c++,C++,以下代码导致gcc(Debian 6.3.0-18+deb9u1)6.3.0 20170516上出现SEGFULT #include <iostream> template<typename LogT> class logger { public: logger(LogT& log) : log_(log) {} template<typename T> LogT& operator<<(T const&
#include <iostream>
template<typename LogT> class logger {
public:
logger(LogT& log) : log_(log) {}
template<typename T> LogT& operator<<(T const& t) {return log_ << "\n> " << t;}
private:
LogT& log_;
};
template<typename LogT> class A {
public:
void f() {
alog << "world";
}
static LogT& alog;
};
logger<std::ostream> alog(std::cout);
template<> logger<std::ostream>& A<logger<std::ostream>>::alog = alog;
int main() {
alog << "hello";
A<logger<std::ostream>>().f();
return 0;
}
#包括
模板类记录器{
公众:
日志记录器(LogT&log):日志(log){
模板日志和操作员
private:LogT&log;
除非我确切知道自己在做什么,否则我不会将引用保留为成员变量。
template<> logger<std::ostream>& A<logger<std::ostream>>::alog = alog;
template<> logger<std::ostream>& A<logger<std::ostream>>::alog = ::alog;