Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is direct-list-initialization with auto considered bad or not preferred?

I've come into the habit of writing code with direct-list-initialization like below as it's more effective and it's very useful to prevent implicit narrowing:

int i {0};
string s {""};
char c {'a'};
bool b {false};

auto num {100}; // But this??

But when it comes to the auto specifier, I have heard it is considered bad or not preferred to write it like that, why is that?

like image 817
Andreas DM Avatar asked Apr 27 '15 08:04

Andreas DM


1 Answers

Here's an example of where using that syntax fails:

struct Foo{};

void eatFoo (const Foo& f){}

int main() {
    Foo a;
    auto b{a};
    eatFoo(b);
}

You might expect this to be fine: b should be a Foo and be passed to eatFoo. Unfortunately, this results in the following compiler error:

prog.cpp:11:10: error: invalid initialization of reference of type 'const Foo&' from expression of type 'std::initializer_list<Foo>'
  eatFoo(b);

As you can see, b is actually of type std::initializer_list<Foo>. Certainly not what we want in this case. If we change it to auto b = a, this works fine. Then if we want to still use auto, but explicitly state the type, we can change it to auto b = Foo{a} and let the compiler elide the copy.

like image 73
TartanLlama Avatar answered Nov 12 '22 19:11

TartanLlama