我正在重载运算符 << 为类实现类似接口的流:
template<typename T>
CAudit& operator << ( const T& data ) {
audittext << data;
return *this;
}
CAudit& operator << ( LPCSTR data ) {
audittext << data;
return *this;
}
模板版本无法编译,出现“致命错误 C1001:内部编译器错误(编译器文件 'msc1.cpp',第 1794 行)”。非模板函数都可以正确编译。
这是由于处理模板时 VC6s 的缺陷,有没有办法解决这个问题?
谢谢,帕特里克
编辑 :
完整的课程是:
class CAudit
{
public:
/* TODO_DEBUG : doesn't build!
template<typename T>
CAudit& operator << ( const T& data ) {
audittext << data;
return *this;
}*/
~CAudit() { write(); }//If anything available to audit write it here
CAudit& operator << ( LPCSTR data ) {
audittext << data;
return *this;
}
//overload the << operator to allow function ptrs on rhs, allows "audit << data << CAudit::write;"
CAudit& operator << (CAudit & (*func)(CAudit &))
{
return func(*this);
}
void write() {
}
//write() is a manipulator type func, "audit << data << CAudit::write;" will call this function
static CAudit& write(CAudit& audit) {
audit.write();
return audit;
}
private:
std::stringstream audittext;
};
问题出现在 operator << 的函数重载上,它用于允许 write() 用作流操纵器:
CAudit audit
audit << "Billy" << write;