Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 铿锵++;使用CRTP时不接受使用模板参数_C++_Templates_C++11_Clang_Crtp - Fatal编程技术网

C++ 铿锵++;使用CRTP时不接受使用模板参数

C++ 铿锵++;使用CRTP时不接受使用模板参数,c++,templates,c++11,clang,crtp,C++,Templates,C++11,Clang,Crtp,在CRTP中使用模板参数时,尝试调用派生初始化列表中的基类构造函数时,我遇到编译错误 问题可以通过以下代码片段复制: template <template<class> class Derived, class T> struct base { }; template <class T> struct derived : public base<derived, T> { derived() : base<deriv

在CRTP中使用模板参数时,尝试调用派生初始化列表中的基类构造函数时,我遇到编译错误

问题可以通过以下代码片段复制:

template <template<class> class Derived, class T>
struct base
{
};

template <class T>
struct derived : public base<derived, T>
{
    derived()
        : base<derived, T>()
    { }
};
bug.cpp:10:16: error: template argument for template template parameter must be a class template or type alias template
        : base<derived, T>()
               ^
bug.cpp:10:11: error: expected class member or base class name
        : base<derived, T>()
          ^
bug.cpp:10:11: error: expected '{' or ','
3 errors generated.
模板
结构基
{
};
模板
结构派生:公共基
{
派生的()
:base()
{ }
};
违规错误消息:

template <template<class> class Derived, class T>
struct base
{
};

template <class T>
struct derived : public base<derived, T>
{
    derived()
        : base<derived, T>()
    { }
};
bug.cpp:10:16: error: template argument for template template parameter must be a class template or type alias template
        : base<derived, T>()
               ^
bug.cpp:10:11: error: expected class member or base class name
        : base<derived, T>()
          ^
bug.cpp:10:11: error: expected '{' or ','
3 errors generated.
bug.cpp:10:16:错误:模板模板参数的模板参数必须是类模板或类型别名模板
:base()
^
bug.cpp:10:11:错误:应为类成员或基类名称
:base()
^
bug.cpp:10:11:错误:应为“{”或“,”
产生了3个错误。
这个问题似乎只发生在clang(3.4)上,而不是g++(4.8、4.7、4.6)上

这是我第一次需要使用带有模板参数的CRTP。我做得好吗?这是clang++的问题吗

最近,我越来越相信clang++错误消息,而不是g++错误消息!

您的代码是合法的

根据C++11标准第14.6.1节:

像普通(非模板)类一样,类模板有一个注入的类名(第9条).注入的类名可用作模板名或类型名。当它与模板参数列表一起使用时,用作模板参数的模板参数,或作为友元类模板声明的详细类型指定中的最终标识符,它引用到类模板本身

看起来您的
clang
版本仍在实现旧规则。根据您的其他注释,它可能仅在ctor初始值设定项列表中执行


用户为尚未完全实现C++11注入类名规则的编译器提供了一种变通方法。请使用任何未经限定的类名,例如:

derived()
    : base< ::derived, T >()
//          ^^ qualified with global namespace
{ }
derived()
:base<::派生,T>()
//^^使用全局命名空间限定
{ }
某些编译器在继承列表中也可能需要此选项:

template <class T>
struct derived : public base< ::derived, T >
//                            ^^
模板
结构派生:公共基<::派生,T>
//                            ^^

+1不知道这在C++11中发生了变化。您可能想在现在已删除的答案中提供我的解决方法。@DavidRodríguez dribeas:不用担心,新标准仍然经常让我感到惊讶。我不担心,相反,我学到了一些东西。我会担心我没有学到任何新东西的那一天:)请注意,在C++03中您需要什么。@Jesse:哇,C++11对Maximum munch的情况做了一个例外:“否则,如果接下来的三个字符是
,那么