匿名临时对象和类模板参数推断——gcc与clang的区别

7
请看下面的代码片段:
```html ```
考虑以下代码片段:
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的一个bug,还是标准中有什么阻止了无名临时对象触发类模板参数推导?

1个回答

8

Clang漏洞 (#34091)

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

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 ]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接