[英]Function template argument deduction works in gcc but not in msvc and clang
I am learning C++ templates using the resource listed here .我正在使用此处列出的资源学习 C++ 模板。 In particular, read about template argument deduction .
特别是,阅读模板参数推导。 Now, after reading, to further clear up my concept of the topic I tried the following example that compiles in gcc but not in clang and msvc.
现在,阅读后,为了进一步理清我对主题的概念,我尝试了以下示例,该示例在 gcc 中编译,但在 clang 和 msvc 中编译。 Demo
演示
template<typename T = int> void f()
{
}
template<typename T> void func(T)
{
}
int main()
{
func(f); //works in gcc but not in clang and msvc
func(f<>); //works in all
}
As we can see, the above example compiles fine in gcc but not in clang and msvc.正如我们所见,上面的示例在 gcc 中编译得很好,但在 clang 和 msvc 中编译得不好。 My question is which compiler is right here according to the latest standard?
我的问题是根据最新标准,这里有哪个编译器?
This is CWG 2608 and the program is well-formed so that gcc is correct in accepting the program.这是CWG 2608并且程序格式正确,因此gcc 在接受程序时是正确的。
From temp.arg.explicit#4 which was added due to cwg 2608 , the default template argument can be used and the empty template argument list <>
can be omitted.从由于 cwg 2608添加的temp.arg.explicit#4中,可以使用默认模板参数,并且可以省略空模板参数列表
<>
。
If all of the template arguments can be deduced or obtained from default template-arguments , they may all be omitted;
如果模板 arguments 都可以从默认的模板参数中推导出或得到,则可以全部省略; in this case, the empty template argument list <> itself may also be omitted.
在这种情况下,空模板参数列表 <> 本身也可以省略。
(emphasis mine) (强调我的)
Note the bold highlighted part in the above quoted statement, which means that the empty template argument list <>
can be omitted in your example because all of the template arguments can be obtained from default template-arguments.注意上面引用语句中的粗体突出部分,这意味着在您的示例中可以省略空模板参数列表
<>
,因为所有模板 arguments 都可以从默认模板参数中获得。
Further, over.over#3 can also be used here to see that the specialization generated from temp.arg.explicit is added to the overloaded set:此外,这里也可以使用over.over#3来查看从temp.arg.explicit生成的特化被添加到重载集合中:
The specialization, if any, generated by template argument deduction ([temp.over], [temp.deduct.funcaddr], [temp.arg.explicit]) for each function template named is added to the set of selected functions considered.
对于每个命名的 function 模板,由模板参数推导([temp.over]、[temp.deduct.funcaddr]、[temp.arg.explicit])生成的特化(如果有)被添加到所考虑的选定函数集中。
(emphasis mine) (强调我的)
This means that at the end, the call func(f);
这意味着最后,调用
func(f);
is well-formed and uses the generated specialization f<int>
.格式正确并使用生成的特化
f<int>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.