考虑以下代码片段:
template <typename T>
struct foo
{
foo(T) { }
};
int main()
{
foo{0};
}
g++ 7 愉快地创建了一个 foo
类型的临时对象,推导出 T = int
。
clang++ 5 和 6 拒绝编译代码:
error: expected unqualified-id foo{0}; ^
这是一个 clang 错误,还是标准中有什么东西阻止类模板参数推导为未命名的临时对象启动?
最佳答案
Clang 错误 ( #34091 )
A placeholder for a deduced class type can also be used in [...] or as the simple-type-specifier in an explicit type conversion (functional notation). A placeholder for a deduced class type shall not appear in any other context. [ Example:
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
— end example ]
关于c++ - 匿名临时对象和类模板参数推导 - gcc vs clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47614027/