首页 > 解决方案 > 使用非类型模板参数专门化模板模板参数

问题描述

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

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 会报告这个编译器错误呢?

标签: c++gccg++clangclang++

解决方案


在 Clang bugzilla 上报告了一个错误,标题为:别名模板产生看似虚假的“模板模板参数具有不同的模板参数”错误

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

在此处查看演示

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


推荐阅读