Why is the std::initializer_list constructor preferred when using a braced initializer list?

前端 未结 3 1737
借酒劲吻你
借酒劲吻你 2020-12-01 07:50

Consider the code

#include 

class Foo
{
    int val_;
public:
    Foo(std::initializer_list il)
    {
        std::cout <<          


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-01 08:22

    The whole initializer list thing was meant to enable list initialisation like so:

    std::vector v { 0, 1, 2 };
    

    Consider the case

    std::vector v { 123 };
    

    That this initializes the vector with one element of value 123 rather than 123 elements of value zero is intended.

    To access the other constructor, use the old syntax

    Foo foo(10);
    

提交回复
热议问题