简体   繁体   English

如何在不使用 std::initializer_list 的情况下定义初始化列表构造函数?

[英]How to define initializer list constructor without using std::initializer_list?

I want to make a small library of my own containers without relying on STL at all, as a form of exercise.我想为自己的容器创建一个小型库,完全不依赖 STL,作为一种练习。 How can I define an initializer list constructor for my classes without std::initializer_list?如何在没有 std::initializer_list 的情况下为我的类定义初始化列表构造函数? How is it even implemented?它是如何实施的?

How can I define an initializer list constructor for my classes without std::initializer_list ?如何在没有std::initializer_list情况下为我的类定义初始化列表构造std::initializer_list

You don't.你没有。 The very definition of "initializer list constructor" is tied into std::initializer_list . “初始化列表构造函数”的定义与std::initializer_list Just like the result of typeid is tied directly into std::type_info , and the result of sizeof and alignof is a std::size_t .就像typeid的结果直接绑定到std::type_info ,而sizeofalignof的结果是std::size_t

Attempting to use C++ while pretending the standard library in its entirety does not exist is folly.在假装标准库不存在的情况下尝试使用 C++ 是愚蠢的。 There are parts of it you can ignore without consequence, but if you want to have an "initializer list constructor", you have to use std::initializer_list .有些部分您可以忽略而不会产生任何后果,但是如果您想要一个“初始化列表构造函数”,则必须使用std::initializer_list

The C++ standard library is not optional ; C++ 标准库不是可选的 it is not divorced from C++ as a langauge.作为语言,它并没有脱离 C++。 Parts of it are, but std::initializer_list isn't one of those parts.它的一部分是,但std::initializer_list不是这些部分之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM