g++;模板问题 我把我的C++ Windows代码(MSVC和英特尔)移植到Linux(G++)。代码使用了很多模板(我喜欢元编程;-)。但我无法编译此代码: template <class TA> struct A { template <class TAB> struct B; }; template <class TC> struct C {}; template <class TD> struct D { template <class TTD> class T {}; }; template<class TA> template<class TBA> struct A<TA>::B : C<typename D<TA>::T<TBA> > { int foo; }; 模板 结构A { 模板结构B; }; 模板 结构C{}; 模板 结构D { 模板类T{}; }; 模板 模板 结构A::B:C { int foo; };

g++;模板问题 我把我的C++ Windows代码(MSVC和英特尔)移植到Linux(G++)。代码使用了很多模板(我喜欢元编程;-)。但我无法编译此代码: template <class TA> struct A { template <class TAB> struct B; }; template <class TC> struct C {}; template <class TD> struct D { template <class TTD> class T {}; }; template<class TA> template<class TBA> struct A<TA>::B : C<typename D<TA>::T<TBA> > { int foo; }; 模板 结构A { 模板结构B; }; 模板 结构C{}; 模板 结构D { 模板类T{}; }; 模板 模板 结构A::B:C { int foo; };,c++,templates,g++,porting,C++,Templates,G++,Porting,g++告诉我,在A::B的定义中,C类具有无效的模板参数。但是在msvc和intel上它工作得很好!这里有什么问题? 对不起,我不能发布原始代码,因为它太复杂了。但是这个例子实际上是相同的,并且在g++上给出了相同的错误。 多谢各位 更新:我发现问题在于T的TBA参数。g++不喜欢在定义中使用第二个模板。您需要模板关键字 template<class TA> template<class TBA> struct A<TA>::B : C<type

g++告诉我,在A::B的定义中,C类具有无效的模板参数。但是在msvc和intel上它工作得很好!这里有什么问题? 对不起,我不能发布原始代码,因为它太复杂了。但是这个例子实际上是相同的,并且在g++上给出了相同的错误。 多谢各位


更新:我发现问题在于T的TBA参数。g++不喜欢在定义中使用第二个模板。

您需要
模板
关键字

template<class TA>
    template<class TBA>
struct A<TA>::B : C<typename D<TA>::template T<TBA> >
{
    int foo;
};

我以前见过“template-template-struct…”语法,但我从来不知道它是什么意思,也不知道它为什么是合法语法。它是什么意思(当“template”在这样的结构前面被提到两次)?@Dennis:它需要在封闭模板之外定义嵌套模板,请参见示例。TA是a的模板参数,TAB是a::Bok的模板参数,我知道了。我考虑过第二个“typename”关键字,但现在是一个“模板”。哇,我真的很喜欢这个诊断消息,现在这就是您对理想编译器的期望。
main1.cpp:21:37: error: use 'template' keyword to treat 'T' as a dependent template name
struct A<TA>::B : C<typename D<TA>::T<TBA> >
                                    ^
                                    template 
1 error generated.