Why does C++ list initialization also take regular constructors into account?

后端 未结 2 1711
谎友^
谎友^ 2021-01-18 07:49

In C++ when using initializer_list syntax to initialize an object, the regular constructors of the object also participate in overload resolution, when no other list initial

2条回答
  •  忘掉有多难
    2021-01-18 08:38

    I don't like this style at all, as it makes me think the object does not have a regular constructor.

    If it's an aggregate type, aggregate initialization is performed. Otherwise, it considers the constructors. If it makes you think that the class is an aggregate, that is not a language problem.

    Is there a reason to now prefer this syntax over regular constructor calls?

    If you're a proponent of uniform initialization yes. If you're not, you can stick with the old style. Note that the other answer talks about std::initializer_list but this doesn't apply to your question whatsoever, since you have no constructor which takes a std::initializer_list. Braced-init-lists and std::initializer_list are separate concepts. It's best not to get them confused this early on.

提交回复
热议问题