且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

递归调用可变参数模板函数重载时的歧义调用

更新时间:2023-11-30 22:24:40

函数模板重载

有有很多规则可以告诉您哪些模板功能更专业(根据给定的参数)。

There is lot of rules to tell which template functions is more specialized (according to given parameters).

关键点

template<typename> void foo();
template<typename, typename...> void foo();

对于 foo< int>() ,但不是

template<typename T> void foo(T);
template<typename T, typename... Ts> void foo(T, Ts...);

用于 foo(42)


如果出现平局,如果一个功能模板具有尾随参数包,而另一个则没有,则带有省略的参数被认为比空参数包更专业。

In case of a tie, if one function template has a trailing parameter pack and the other does not, the one with the omitted parameter is considered to be more specialized than the one with the empty parameter pack.