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

我与影子孤独终老i 提交于 2020-01-01 08:50:09

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29890918/why-is-direct-list-initialization-with-auto-considered-bad-or-not-preferred

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!