我正在尝试对类模板进行部分特化,其中一个模板参数是包含单个非类型参数的模板模板参数。例如:
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会报这个编译错误呢?
最佳答案
在 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 开始)。
关于c++ - 使用非类型模板参数专门化模板模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54976674/