C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 模板代码上的编译器堆栈溢出大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在处理我自己的类型擦除迭代器时,我遇到了一个问题,编译器(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,char> a;
    a.a();
    a.c();
    any<int,char> b;
    b.a();
    b.b();
    return 0;
}

显然,我已经删除了我可以在哪里留下bug. (原始代码为780行)删除任何剩余的模板参数会导致代码编译.

完整的错误消息是:

@H_887_2@main.cpp(23): Fatal error C1063: compiler limit : compiler stack overflow main.cpp(20) : see reference to class template instantiation 'from<basetype,valuetype>' being compiled

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

解决方法

作为一种解决方法,虑在非专业化的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>
{
};

以下完整程序应该编译而不会出错:

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 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,valuetype>
        : public base  //commenTing out _either_ of these makes it compile,valuetype>
{}; //this is line 23,where the compiler crashes

int main() {return 0;}

大佬总结

以上是大佬教程为你收集整理的c – 模板代码上的编译器堆栈溢出全部内容,希望文章能够帮你解决c – 模板代码上的编译器堆栈溢出所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。