以下代码可在 gcc 4.7.2 和 MSVC-11.0 中编译:
template <typename T>
void foo(T bar) {}
template <typename T, typename... Args>
void foo(T bar, Args... args) {}
int main()
{
foo(0); // OK
}
为什么?我认为这一定是一个不明确的调用:
ISO/IEC 14882:2011
14.5.6.2 函数模板的部分排序 [temp.func.order]
5 ...
[ Example:
template<class T, class... U> void f(T, U...); // #1
template<class T > void f(T); // #2
template<class T, class... U> void g(T*, U...); // #3
template<class T > void g(T); // #4
void h(int i) {
f(&i); // error: ambiguous
g(&i); // OK: calls #3
}
—end example ]
这被认为是当前标准的缺陷。甚至标准本身也依赖于非可变参数模板在规范中的可变参数模板之前进行部分排序std::common_type
:
§20.9.7.6 [meta.trans.other] p3
嵌套的 typedefcommon_type::type
应定义如下:
template <class ...T> struct common_type;
template <class T>
struct common_type<T> {
typedef T type;
};
template <class T, class U>
struct common_type<T, U> {
typedef decltype(true ? declval<T>() : declval<U>()) type;
};
template <class T, class U, class... V>
struct common_type<T, U, V...> {
typedef typename common_type<typename common_type<T, U>::type, V...>::type type;
};
具体来说common_type<T, U>
vs common_type<T, U, V...>
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)