将代码从gcc移植到clang

Porting code from gcc to clang

本文关键字:clang gcc 代码      更新时间:2023-10-16

你好,我正试图使我的代码在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>)的实例化,而不是模板本身。