假设你的代码“无效的模板参数”错误,但不是GCC
template <template<class> class BaseType>
class EST16
: public BaseType<int>
{
public:
EST16(double d)
{
}
};
template <class T>
class SCEST
{
T y;
};
typedef EST16<SCEST> EST16_SC;
class Child
: public EST16_SC
{
public:
Child()
: EST16_SC(1.0)
{
}
};
class NotWorkingChild
: public EST16<SCEST>
{
public:
NotWorkingChild()
: EST16<SCEST>(1.0)
{
}
};
TEST(TemplateTest, TestInstantiate)
{
Child child;
NotWorkingChild notWorkingChild;
}
儿童和NotWorkingChild只有typedef的不同。在GCC编译两种,在Visual Studio NotWorkingChild的构造产生以下错误:
2>..\..\..\src\itenav\test\SCKFErrorStateTest.cpp(43) : error C3200: 'SCEST<T>' : invalid template argument for template parameter 'BaseType', expected a class template
2> with
2> [
2> T=int
2> ]
你能解释一下为什么是这样的情况?有没有比typedef更好的便携式解决方案?
谢谢!
你有没有写过任何实际实例化和使用模板类的代码?我的意思是说,“主”。可能发生的一件事情是,VCC在看到'typedef'时测试实例化,而GCC在实际使用其中一个子类之前不会感到烦恼。如果那是真的,那么只要你真正编写使用其中一个子类模板的代码,你也会在GCC中得到错误。 – phooji 2011-03-15 17:09:13
是的,我做到了。 TEST宏创建一个实际从main()调用的Google测试。 – Philipp 2011-03-15 17:25:50
对不起 - 'TEST'宏是在我滚动截断之后,所以我错过了它。我不是100%为什么会失败,但是我已经发布了一个替代方法,它应该在编译器中更具可移植性。这也是更可读,在我的愚蠢观点:) – phooji 2011-03-15 17:39:16