c++ - 将代码从 gcc 移植到 clang

标签 c++ templates parameters c++11 clang

您好,我正在尝试让我的代码在 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/

相关文章:

c++ - 需要额外模板参数的类型的函数的部分模板特化

scala - 为什么 Option 的 orNull 方法有这个多余的隐式参数?

c++ - 可以在应用程序代码中复制数据库数据吗?

c++ - 掌握C++为大二做准备: How?

c++ - Mktime 返回不同的值

Swift 初学者需要详细说明语法 - 传递和返回函数

asp.net-mvc-2 - 将参数传递给 MVC Ajax.ActionLink

c++ - Boost Graph Library 动态边权重

c++ - 具有完全专用别名的实现切换

C++ 递归类型特征