Initializer list syntax in member initializer list using C++11

前端 未结 3 960
孤街浪徒
孤街浪徒 2020-12-01 17:47

I\'ve been going through \'A Tour of C++\' and Bjarne uses the the c++11 initializer list feature in member initialization in a constructor, like so (using curly brackets):<

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 18:19

    The short description is: the notation in the member initializer list matches that of variables initialized elsewhere. Sadly, the description of what it does is not as easy at all because there are two somewhat conflicting changes relating to the use of curly braces for constructor calls:

    1. The unified initialization syntax was intended to make to make have all constructions use curly braces and it would just call the corresponding constructor, even if it is the default argument or the type doesn't have a constructor at all and direct initialization is used.
    2. To support variable number of arguments, curly braces can be used to provide an std::initializer_list without an extra pair of parenthesis/curly braces. If there is a constructor taking an std::initializer_list (for a suitable type T) this constructor is used when using curly braces.

    Put differently, if there is no std::initializer_list constructor but some other user defined constructor the use of parenthesis and curly braces is equivalent. Otherwise it calls the std::initializer_list constructor. ... and I guess, I'm missing a few details as the entire initialization is actually quite complicated.

提交回复
热议问题