匿名临时和类模板参数扣除-GCC与Clang

Anonymous temporaries and class template argument deduction - gcc vs clang

本文关键字:参数 -GCC Clang      更新时间:2023-10-16

考虑以下代码段:

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

wandbox上的实时示例


这是一个clang虫的错误,还是标准中的某些东西可以防止 class template参数扣除启动未命名的临时性?

clang bug(#34091)

来自[dcl.type.class.deduct]:

推导类型类型的占位符也可以在[...] 或作为显式类型转换(功能符号)中的[...] 或作为 simple-type-specifier 。推论类型类型的占位符不得出现在任何其他情况下。 [示例:

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

- 结束示例]