模板代码上的编译器堆栈溢出

Moo*_*uck 7 c++ templates visual-c++ visual-c++-2010

在我自己的类型擦除迭代器上工作时,我遇到了一个问题,编译器(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;
}
Run Code Online (Sandbox Code Playgroud)

显然,我已经删除了我可以在哪里留下bug.(原始代码是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
Run Code Online (Sandbox Code Playgroud)

IDEOne编译得很好.我听说MSVC实现了两阶段查找错误,这似乎是相关的,但是没有解释为什么当我删除from继承的行时它会编译base. 任何人都可以教我为什么MSVC10不会编译这个?我应该避免做什么?

Jam*_*lis 1

作为解决方法,请考虑在非专业化any和专业化之间引入一个附加类category = void

template <class valuetype>
class detail_void_any
    : public any<void, valuetype>
{
};


template<class category, class valuetype>
class any
    : public detail_void_any<valuetype>
{
};
Run Code Online (Sandbox Code Playgroud)

以下完整程序应编译无错误:

class base {};      // Data Holder (in reality it's templated, so required)
template<class category, class valuetype>  
        class any;  // Virtual Function Interface
template<class basetype, class category, class valuetype> 
        class from; // Virtual Function Implementation

template<class valuetype>
class any<void,valuetype>
{};


template <class valuetype>
class detail_void_any
    : public any<void, valuetype>
{
};

template<class category, class valuetype>
class any
    : public detail_void_any<valuetype>
{
};

template<class basetype, class valuetype>
class from<basetype,void,valuetype>
        : public base  //commenting out _either_ of these makes it compile
        , public any<void,valuetype>
{}; //this is line 23, where the compiler crashes

int main() {return 0;}
Run Code Online (Sandbox Code Playgroud)