匿名临时变量和类模板参数推导 - 的gcc VS铛
问题描述:
考虑下面的代码片断:匿名临时变量和类模板参数推导 - 的gcc VS铛
template <typename T>
struct foo
{
foo(T) { }
};
int main()
{
foo{0};
}
克++ 7愉快地创建foo
类型的临时对象,推导T = int
。
铛++ 5和6拒绝编译的代码:
error: expected unqualified-id foo{0}; ^
这是一个铛错误,或者是有一些在标准防止类模板参数扣除从踢入未命名的临时文件ES?
答
锵蝽(#34091)
一种用于推导的类类型的占位符也可以在使用[...] 或作为简单型说明符在explicit type conversion (functional notation)。推导出的类类型的占位符不应出现在任何其他上下文中。 [实施例:
template<class T> struct container { container(T t) {} template<class Iter> container(Iter beg, Iter end); }; template<class Iter> container(Iter b, Iter e) -> container<typename std::iterator_traits<Iter>::value_type>; std::vector<double> v = { /* ... */ }; container c(7); // OK, deduces int for T auto d = container(v.begin(), v.end()); // OK, deduces double for T container e{5, 6}; // error, int is not an iterator
- 端示例]