您好,我正在尝试让我的代码在 clang 3.2-9 下编译,这是我无法编译的简化示例:
template<template <class>class Derived, typename Type>
class Foo
{
public:
Foo(){}
};
template<typename Type>
class Bar
: public Foo<Bar, Type>
{
public:
Bar()
: Foo<Bar, Type>()
{}
};
int main()
{
Bar<int> toto;
}
这是 clang 告诉我的错误:
test.cpp:14:19: error: template argument for template template parameter must be a class template
: Foo<Bar, Type>()
^
test.cpp:14:15: error: expected class member or base class name
: Foo<Bar, Type>()
^
test.cpp:14:15: error: expected '{' or ','
3 errors generated.
在gcc 4.7.2下编译没有问题。而且我无法获得使其在 clang 下工作的正确语法。 有人可以帮助我吗,我有点卡住了......
最佳答案
只需为您的类模板使用完全限定的名称:
template<template <class> class Derived, typename Type>
class Foo
{
public:
Foo(){}
};
template<typename Type>
class Bar
: public Foo<::Bar, Type>
// ^^^^^
{
public:
Bar()
: Foo<::Bar, Type>()
// ^^^^^
{}
};
int main()
{
Bar<int> toto;
}
问题是在Bar
里面, 名字 Bar
指类本身,即 Bar
的实例化类模板(即 Bar<Type>
)而不是模板本身。
你可以看到这个例子编译here .
关于c++ - 将代码从 gcc 移植到 clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15245326/