Implementation of std::initializer_list

后端 未结 3 1403
旧巷少年郎
旧巷少年郎 2021-02-04 01:46

I have been looking at how the initializer_list is implemented so I found section 18.9 of the standard and found a simple enough looking interface. I thought it wou

3条回答
  •  太阳男子
    2021-02-04 02:00

    The name is important because the standard says it is. The standard needs some way for you to be able to say, "this constructor can be passed a braced-init-list containing a sequence values of the type T". That way was given the name "std::initializer_list".

    You cannot make a class that has all of the language properties of initializer_list. You can make one that satisfies the conditions of the type specified by section 18.9 of the standard. But you'll notice that the only constructor specified there is a default constructor. The only way to create an initializer_list with actual elements relies on the compiler, not user-land code.

    So you can't replicate everything about initializer_list. Just as you can't replicate std::type_info. The C++ standard library is not optional.

提交回复
热议问题