【发布时间】:2012-01-19 17:29:16
【问题描述】:
在处理我自己的类型擦除迭代器时,我遇到了一个问题,即编译器 (MSVC10) 因此代码上的堆栈溢出而崩溃:
struct base {}; //In actual code, this is a template struct that holds data
template<class category, class valuetype>
struct any; //In actual code, this is abstract base struct
template<class basetype, class category, class valuetype>
struct from; //In actual code, this is function definitions of any
template<class valuetype>
struct any<void,valuetype>
{ void a() {} };
template<class category, class valuetype>
struct any
: public any<void,valuetype> //commenting this line makes it compile
{ void b() {} };
template<class basetype, class valuetype>
struct from<basetype,void,valuetype>
: public base //commenting out _either_ of these makes it compile
, public any<void,valuetype>
{ void c() {} };
int main() {
from<int, void, char> a;
a.a();
a.c();
any<int, char> b;
b.a();
b.b();
return 0;
}
很明显,我已经删除了所有可能存在错误的地方。 (原始代码是 780+ 行)删除任何剩余的模板参数会导致代码编译。
完整的错误信息是:
main.cpp(23): fatal error C1063: compiler limit : compiler stack overflow
main.cpp(20) : see reference to class template instantiation 'from<basetype,void,valuetype>' being compiled
IDEOne compiles it fine。我听说 MSVC 实现了错误的两阶段查找,这似乎很相关,但没有解释为什么当我删除使 from 继承自 base 的行时它会编译。 谁能教我为什么 MSVC10 不能编译这个?我做了什么我应该避免的事情?
【问题讨论】:
-
不管怎样,GCC 4.6 编译您的示例代码没有任何问题(在 Linux/Debian/Sid/AMD64 上)。也许你可以切换到 GCC(例如一些 MinGW 或 Cygwin 变体......)?
-
我实际上通过 IDEOne 进行了相当多的调试,但是我的命令行 fu 和 linux 很弱,所以 Cygwin 很吓人。不过我开始玩弄它了。
-
你有一个继承自自身的模板类?还是我读错了?
-
确保获得 GCC 4.6,因为它在符合最新的 C++ 标准方面取得了很大进展...
标签: c++ templates visual-c++ visual-c++-2010