简体   繁体   中英

STL independent initializer list

I am little bit confused about initializer lists.

I think that initializer list is something what is part of C++:

struct Something { char c0, c1; };

Something something = { 'H', 'W' };

Analogically with arrays etc. And there is something connected (in my eyes) with STL (in header <initializer_list> ), called std::initializer_list<T> , which matches initializer lists as eg method params, etc.

std::initializer_list<char> list = { 'H', 'e', 'l', 'l', 'o' };

Now my confusion:

auto match = { 'H', 'e', 'l', 'l', 'o' };

When I look what is auto interpretation (in MSVC 2015), it is std::initializer_list<char> .

So my questions are:

Where is the line between C++ language and STL library?

Is there some directive specifying how should be initializer list (in C++ meaning) interpreted?

Could be interpretation changed eg from std::initializer_list<T> into T[] ?

This is an aggregate initialization

struct Something { char c0, c1; };
Something something = { 'H', 'W' };

Where is the line between C++ language and STL library?

This is not really a difference between C++ language and STL library since the standard library is itself written in C++ (it makes no sense to think of them as two separate entities). The main point is in distinguishing which kind of element you're initializing and what constructors are available.

Most of the confusion stems from the fact that proposals and defect reports have stirred quite a bit of arguing on how curly-braces initialization are to be interpreted (cfr. Scott Meyers articles , some of them have been recently discussed as well).

Is there some directive specifying how should be initializer list (in C++ meaning) interpreted?

The only proper way to understand it is in being able to recognize the types you're using and understanding the C++ standard directives (according to the C++ version and compiler support you're using).

Answers partially based on MSDN thread .

Where is the line between C++ language and STL library?

The line between standard library and pure C++ is a little bit noisy. It is situated in part of library called LSL, Language Support Library. The line exist in following example: Allocation returns std::bad_alloc . So to be pure from c++ language viewpoint, std::bad_alloc could be supressed using direct allocation via OS API. Similarly with std::size_t, std::nullptr_t, etc...

In the following example, the line, if exists, is noisy: dynamic_cast throws std::bad_cast . And similar example of indipensibility LSL and C++ is initializer list.

Is there some directive specifying how should be initializer list (in C++ meaning) interpreted?

Directive as language specification exists, directive as kind of #pragma directive or whatever what could be simply changed does not.

Could be interpretation changed eg from std::initializer_list into T[]?

In MSVC, no. Probably nowhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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