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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With