前向声明 类模板针对模板类的特化 错误

模板特化,特化类型必须要在当前编译单元内确定.

A文件中,有如下声明:

template<class> childtype; //前向声明特化类型

template<class host> initialconverter //主模板
{
    typedef host type;
}

template<class host> 
initialconverter< childtype<host> >  //特化
{
    typedef childtype<host> type; 
}

然后你在B文件中(B在另外的编译单元)实现childtype或者include了childtype的实现头文件,则在B文件中intialconverter<childtype<someclass>>的特化是无法编译通过的.

每个编译单元中,类模板都生成了单独的实例,应是此原因.

解决办法是,将initialconverter< childtype<host> >在A文件中移除, 在B文件中特化.或者A文件中全部实现(include).

感觉都不好,未能查找到较好的方法,碰到这个应是自己的设计上出了问题(只是想节省下编译时间).备忘.

原文地址:https://www.cnblogs.com/flytrace/p/2889632.html