可变的模糊调用
variadic ambiguous call
下面的代码可以同时在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. function .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
嵌套的typepedef
common_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...>
是的,你是对的!这是编译器的一个"特性",而且很可能是一个故意的特性,因为委员会在第1395期中建议,这种情况应该被接受,因此,在未来的标准(甚至是TR)中,很可能会。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 类X有多个默认构造函数和对重载函数的模糊调用
- 对C++基类的模糊函数调用
- C++中的构造函数调用模糊性
- Visual Studio不允许我使用sqrt或floor,对重载函数的模糊调用
- 修复C++多继承模糊调用
- 在Windows上为Matlab构建libspline - 对重载函数'pow'的模糊调用
- Clang模糊调用(int、float、double)
- 对可变模板的调用是模糊的
- 自定义成员检测器中的模糊调用
- Visual Studio 2012中嵌套boost::assign:list_of失效-对重载函数的模糊调用
- 编译boost时的模糊调用.type_erasure与VS 2015社区
- 可变的模糊调用
- 模板函数调用模糊性错误