匿名临时和类模板参数演绎 - gcc vs clang

时间:2017-12-03 00:26:53

标签: c++ language-lawyer c++17 class-template argument-deduction

请考虑以下代码段:

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};
       ^

live example on wandbox

这是一个铿锵的错误,还是标准中的某些东西会阻止类模板参数推断为未命名的临时工作而踢?

1 个答案:

答案 0 :(得分:8)

Clang bug(#34091

来自[dcl.type.class.deduct]

  

推断类类型的占位符也可以在[...] 中使用,或者在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
     

- 结束示例]