问题描述
当在CRTP中使用模板模板参数时,尝试调用派生初始化列表中的基类构造函数时,我会收到编译错误。
I'm getting compilation errors when trying to call the base class constructor in derived initialization list when using a template template parameter with 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.
这个问题只出现在clang(3.4),而不是g ++(4.8,4.7,4.6) 。我也用-std = c ++ 11编译。
This problem only appears to happen on clang (3.4), not g++ (4.8, 4.7, 4.6). I'm compiling with -std=c++11 also.
这是我第一次使用CRTP和模板模板参数。我这样做好了,这是clang ++的问题吗?
This is the first time I've needed to use CRTP with template template parameter. Am I doing this okay and it's a problem with clang++ or not?
我已经成长为信任clang ++错误消息超过g ++的晚了!
I've grown to trust clang++ error messages more than g++ of late!
从C ++ 11标准中,第14.6节。$ b
推荐答案
1:
看起来像您的版本 clang
仍在实施旧规则。根据您的其他意见,它只能在 ctor-initializer-list 中执行。
Looks like your version of clang
is still implementing the old rule. Based on your additional comments, it might be doing so only in the ctor-initializer-list.
用户为没有完全实施C +的编译器提供了一个解决方法, +11 inject-class-name规则。使用非限定类的任何名称,例如:
User David Rodríguez - dribeas provided a workaround for compilers that haven't fully implemented the C++11 injected-class-name rule. Use any name of the class that isn't unqualified, for example:
derived()
: base< ::derived, T >()
// ^^ qualified with global namespace
{ }
b $ b
有些编译器可能还需要在继承列表中这样做:
Some compilers may require this in the inheritance list also:
template <class T>
struct derived : public base< ::derived, T >
// ^^
这篇关于clang ++在使用CRTP时不接受使用模板模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!