clang ++在使用CRTP时不接受使用模板模板参数 [英] clang++ not accepting use of template template parameter when using CRTP

查看:337
本文介绍了clang ++在使用CRTP时不接受使用模板模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在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:


类模板与普通(非模板)类一样,具有注入类名(第9条)。 inject-class-name可用作模板名称或类型名称。 使用模板参数列表作为模板模板参数的模板参数时,或作为详细说明类型规范中的最终标识符的朋友类模板声明,它是指类模板本身

Like normal (non-template) classes, class templates have an injected-class-name (Clause 9). The injected-class-name can be used as a template-name or a type-name. When it is used with a template-argument-list, as a template-argument for a template template-parameter, or as the final identifier in the elaborated-type- specifier of a friend class template declaration, it refers to the class template itself.

看起来像您的版本 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.

用户 DavidRodríguez - dribeas 为没有完全实施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时不接受使用模板模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆