Why g ++ 5 outputs an object instead of initializer_list to automatic type inference

I recently came across this code:

struct Foo{}; int main() { Foo a; // clang++ deduces std::initializer_list // g++5.1 deduces Foo auto b{a}; a = b; } 

It compiles with g ++ 5.1, but does not work in clang ++ (it uses both -std=c++11 and -std=c++14 , the same results). The reason is that clang ++ outputs type b as std::initializer_list<Foo> , while g++5.1 outputs as Foo . AFAIK, the type really needs to be (intuitive) std::initializer_list here . Why is g ++ 5 inferring a type like Foo ?

+6
c ++ c ++ 11 clang ++ auto gcc5
May 2 '15 at
source share
1 answer

There is a proposal for C ++ 1z that implements new type inference rules for initializing brackets ( N3922 ), and I assume that gcc implemented them:

To directly initialize a list:
1. For a list with a binding-initialization with only one element, automatic output will be output from this record.
2. For a bit-init list with more than one element, automatic output will be poorly formed.

[Example:

 auto x1 = { 1, 2 }; // decltype(x1) is std::initializer_list<int> auto x2 = { 1, 2.0 }; // error: cannot deduce element type auto x3{ 1, 2 }; // error: not a single element auto x4 = { 3 }; // decltype(x4) is std::initializer_list<int> auto x5{ 3 }; // decltype(x5) is int. 

- end of example]

Here's the gcc patch regarding the new changes to the Unicorn Initialization.

+12
May 02 '15 at 21:34
source share



All Articles