<分区>
同时帮助解决 too many template parameters in template template argument 中提到的问题我脑子里出现了一个问题:在这种情况下,哪个编译器是正确的编译:
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.
显然,即使提供了默认的第三个参数,它也将其视为三参数模板。那么问题来了,哪个编译器是正确的?标准对这种情况有说明吗?
PS 我不需要解决此类问题的方法,因为它非常简单。我只想知道“谁对谁错”