【问题标题】:Specialize template template parameter with a non-type template parameter使用非类型模板参数专门化模板模板参数
【发布时间】:2019-03-04 04:34:07
【问题描述】:

我正在尝试对类模板进行部分特化,其中一个模板参数是包含单个非类型参数的模板模板参数。例如:

template <
    class T, 
    template <class Result, Result> class BinaryOperation
>
struct Foo;

template <
    template <class Result, Result> class BinaryOperation
>
struct Foo<
    int,
    BinaryOperation
>
{ };

这段代码使用 GCC-4.9.2 编译得很好。

但是,使用 Clang-4.0,我收到一条神秘的错误消息:

$ clang++-4.0 test.cpp -o test -std=c++14 -stdlib=libc++ 
test.cpp:18:3: error: template template argument has different template parameters than its corresponding template template
      parameter
                BinaryOperation
                ^
test.cpp:14:33: note: template non-type parameter has a different type 'Result' in template argument
                template <class Result, Result> class BinaryOperation
                                              ^
test.cpp:9:33: note: previous non-type template parameter with type 'Result' is here
                template <class Result, Result> class BinaryOperation

我一直在谷歌搜索此错误消息,但我不清楚。似乎是说Result 在以模板参数的形式出现时与作为特化列表的参数之一出现时被认为是不同的类型。

它适用于 GCC-4.9.2,所以我不知道这是否是 Clang 4 的问题,或者 GCC-4.9.2 是否允许它不应该的东西。

那么为什么 Clang-4 会报告这个编译器错误呢?

【问题讨论】:

  • 也没有来自 gcc 8.3 的投诉,而且我没有发现任何明显的问题。

标签: c++ gcc g++ clang clang++


【解决方案1】:

在 Clang bugzilla 上报告了一个错误,标题为:Alias template produces seemingly bogus "template template argument has different template parameters" error

可以在 Clang 4 中使用编译器选项 -frelaxed-template-template-args 解决此错误。

demo here

这已在 Clang 的更高版本(从 5.0.0 开始)中得到修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多