VC6和模板错误
我正在重载运算符 <<为类实现类似流的接口:
template<typename T>
CAudit& operator << ( const T& data ) {
audittext << data;
return *this;
}
CAudit& operator << ( LPCSTR data ) {
audittext << data;
return *this;
}
模板版本无法编译,并显示“致命错误 C1001:内部编译器错误(编译器文件“msc1.cpp”,第 1794 行)”。非模板函数都可以正确编译。
这是由于 VC6 在处理模板时存在缺陷吗?有没有办法解决这个问题?
谢谢, Patrick
编辑:
完整的类是:
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;
};
问题发生在运算符 << 的函数重载上。它用于允许 write() 用作流操纵器:
CAudit audit
audit << "Billy" << write;
I am overloading operator << to implement a stream like interface for a class:
template<typename T>
CAudit& operator << ( const T& data ) {
audittext << data;
return *this;
}
CAudit& operator << ( LPCSTR data ) {
audittext << data;
return *this;
}
The template version fails to compile with "fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1794)". Non-template functions all compile correctly.
Is this due to VC6s deficiencies when handling templates and is there a way around this?
Thanks,
Patrick
EDIT :
the full class is:
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;
};
The problem occurs with the function overload of operator << which is used to allow write() to be used as a stream manipulator:
CAudit audit
audit << "Billy" << write;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于旧的 Visual Studio 6 来说,函数指针模板的重载肯定是太多了。
作为解决方法,您可以为操纵器和重载运算符定义类型<<对于那种类型。
这是一些代码:
That overload of the template for function pointers surely is too much for good old Visual Studio 6.
As a workaround you could define a type for your manipulator and overload operator<< for that type.
Here's some code:
这种错误看起来确实像是由 VC6 之前的标准模板实现引起的崩溃。
最好的建议当然是升级到 VC7.0、7.1、8.0、9.0 或 10 的测试版。
与 Windows 版本相比,当 Me、2000、XP、Vista 和 7 可用时,它仍然使用 Windows 98。
话虽如此,您可以通过一个简单的技巧来大大简化查找:
这里希望
operator<<
的第一次查找找到单个成员模板。其他运算符<<
候选者不是其他类和内置函数的成员。它们应该明显比这个模板更糟糕。operator
内的第二次查找只需要处理名为print
的CAudit
成员。The kind of error definitely looks like the kind of crashes caused by VC6's pre-previous-standard implementation of templates.
The best advice is of course to upgrade to either VC7.0, 7.1, 8.0, 9.0 or the beta of 10.
To compare that to Windows versions, it's still using Windows 98 when Me, 2000, XP, Vista and 7 are available.
Having said that, you can simplify the lookup a lot by a simple trick:
The hope here is that the first lookup of
operator<<
finds the single member template. The otheroperator<<
candidates are non-members for other classes and built-ins. They should be unambiguously worse that this template. The second lookup inside youroperator
only needs to deal withCAudit
members calledprint
.编辑:
EDIT:
问题似乎不在您发布的代码片段中。该程序运行良好:
您可以发布更多代码吗?
The problem does not seem to be in the code snippet you posted. This program works fine:
Could you post some more code?