Search code examples
c++c++11autolist-initialization

Why would they special-case certain initializer lists instead of treating them all the same?


Say I have a variable auto x that I want to initialize to 7 using brace initialization, simple:

auto x {7};

Except I learned that x is NOT an integer, but an initialization list itself. Why? Is there a specific reason why the committee would decide that auto should grab the initialization list in the case of a single auto value, or do they expect us to just realize these shouldn't be used together. I cant seem to think of a possible reason i would want an initializer list to be stored into auto as opposed to the value


Solution

  • A very practical answer is, "why should int be selected?" Or double, or any UDT with an int single-argument constructor? By declining to deduce a concrete type, the compiler preserves any possible application of the more general initializer list.