模板模板参数导致Clang下的编译器错误,而不是GCC
Template template argument causes compiler error under Clang but not GCC
在帮助解决模板模板参数中模板参数过多的问题时,我脑海中出现了一个问题:在这种情况下,哪个编译器的编译是正确的:
template <template <typename, typename> class Op>
class Function
{
};
template <typename A, typename B, bool is_f = std::is_floating_point<A>::value || std::is_floating_point<B>::value > struct Operator;
template <typename A, typename B>
struct Operator<A, B, false>
{};
template <typename A, typename B>
struct Operator<A, B, true>
{};
using FunctionOperator = Function<Operator>;
int main(int argc, char * argv[]){
std::cout << "hi!n";
return 0;
}
GCC 7+编译它时没有任何错误。Clang 6和更高版本给出的错误表明作为模板参数传递的Operator
模板存在问题:
tmp.cpp:19:35: error: template argument has different template parameters than its corresponding template parameter
using FunctionOperator = Function<Operator>;
^
tmp.cpp:8:1: note: too many template parameters in template template argument
template <typename A, typename B, bool is_f = std::is_floating_point<A>::value || std::is_floating_point<B>::value > struct Operator;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tmp.cpp:3:11: note: previous template template parameter is here
template <template <typename, typename> class Op>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
显然,即使提供了默认的第三个参数,它也将其视为3参数模板。那么问题来了,哪一个编译器是对的?标准对这种情况有什么说明吗?
PS我不需要解决这类问题的方法,因为它非常简单。我只想知道"谁是对的">
由于C++17(CWG 150(,允许模板模板参数使用默认模板参数来匹配模板参数较少的模板模板参数。
template<class T> class A { /* ... */ }; template<class T, class U = T> class B { /* ... */ }; template <class ...Types> class C { /* ... */ }; template<template<class> class P> class X { /* ... */ }; X<A> xa; // OK X<B> xb; // OK in C++17 after CWG 150 // Error earlier: not an exact match X<C> xc; // OK in C++17 after CWG 150 // Error earlier: not an exact match
Operator
有3个模板参数,第三个有默认值,则它可以用作模板参数Op
的参数,即使它只需要两个模板参数。
相关文章:
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- GCC 警告和 gcc 错误消息之间的区别
- 调试模板时出现问题.专门针对 Linux GCC 7、GCC 6、GCC 5、GCC 4.9 错误构建失败:模板参数 1
- typedef名称与可变模板参数名称一致时出现GCC错误
- 条件变量:wait_for.gcc错误
- 不可复制类数据成员的统一初始化导致gcc错误
- 在类中将不完整类型的unique_ptr初始化为 nullptr 时编译 gcc 错误
- GCC-7:错误:无法识别的命令行选项"-M64"
- C++ GCC 错误:"sqrtl"不是"std"的成员
- 在联盟中,不愿命名的未命名结构的成员是错误或GCC错误
- 尾随返回类型中带有 SFINAE 的 GCC 错误
- 与lambda一起使用虚拟继承在初始化列表中捕获此问题的GCC错误
- 通过指向成员的指针对嵌套访问进行编译时评估期间出现 GCC 错误
- 从成员变量访问静态 constexpr 成员,GCC 错误?
- std::seed_seq编译错误(GCC错误?)
- GCC 错误:如果已知函数正常返回,则函数可能是属性"pure"的候选者
- iostream GCC错误,转换为boost::filesystem::iostream for Windows
- GCC错误?链式方法,断开序列点
- 是否存在GCC错误:默认std :: function
- GCC 错误错误的 ELF 类:ELFCLASS64,尽管有 -m64 标志