Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct term for the list that initializes the data members?

  • One colleague says initializer list, another initialization list.
  • One SO answer says initializer list, another initialization list.

Which is the correct* terminology?

PS: They all talk about data member initializer/ation lists.


*If correct is ambiguous to you, you can fall back to the term used be the Standard.

like image 754
gsamaras Avatar asked Nov 23 '18 09:11

gsamaras


People also ask

What is a member initializer list?

Member initializer list is the place where non-default initialization of these objects can be specified. For bases and non-static data members that cannot be default-initialized, such as members of reference and const-qualified types, member initializers must be specified.

Is used to initialize the data members of the class?

The initializer list will just initialize the members to specific values at the beginning of the constructor. Even if a constructor has an initializer list, you are still free to do other work in the body of the constructor.

How do you initialize a data member in C++?

Static Data Member Initialization in C++ We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator, then the variable name. Now we can assign some value.

How do you initialize a class member?

To initialize a class member variable, put the initialization code in a static initialization block, as the following section shows. To initialize an instance member variable, put the initialization code in a constructor.


2 Answers

Which is the correct terminology?

"Correct" being ambiguous, let's see what:

  1. The Standard calls it,
  2. The C++ gurus call it,
  3. The vulgus calls it.

The Standard

[lass.base.init]/1&2

1 In the definition of a constructor for a class, initializers for direct and virtual base class subobjects and non-static data members can be specified by a ctor-initializer, which has the form

ctor-initializer:
  : mem-initializer-list

mem-initializer-list:
  mem-initializer ...opt
  mem-initializer-list , mem-initializer ...opt

mem-initializer:
  mem-initializer-id ( expression-list opt )
  mem-initializer-id braced-init-list

mem-initializer-id:
  class-or-decltype
  identifier

2 In a mem-initializer-id an initial unqualified identifier is looked up in the scope of the constructor's class and, if not found in that scope, it is looked up in the scope containing the constructor's definition. [ Note: If the constructor's class contains a member with the same name as a direct or virtual base class of the class, a mem-initializer-id naming the member or base class and composed of a single identifier refers to the class member. A mem-initializer-id for the hidden base class may be specified using a qualified name. — end note ] Unless the mem-initializer-id names the constructor's class, a non-static data member of the constructor's class, or a direct or virtual base of that class, the mem-initializer is ill-formed.

It's called a mem-initializer-list: this is a technical term I won't use personally.

The C++ guru

I'm currently watching the talks given at the CppCon2018, by likes of Herb Sutter, Kate Gregory, Timur Doumler, John Lakos, ... This is available on Youtube and I suggest you watch it too.

They use the term the initialiser list. Or when it's ambiguous the member initialiser list.

Now, let's compare some search results:

                               +----------------+-------------+
                               | Google scholar | Google book |
+------------------------------+----------------+-------------+
| "member initialization list" | 59 results     | 948 results |
| "member initializer list"    | 34 results     | 553 results |
+------------------------------+----------------+-------------+

On written media, those gurus (well, everybody can write a paper or a book, but gurus tend to write more of those) call it the member initialization list most of the time.

Common C++ programmers

Well, there's the one that don't know what this is, and there's the one I've heard call it the initialiser list. I call it the initialiser list, even when talking in my mother tongue. In French, I've heard collegues call it what would translate to the initialisation list. There's some variation then I guess.

Conclusion

Call it the initialiser list. This is the correct term for me.

like image 124
YSC Avatar answered Oct 27 '22 00:10

YSC


The C++ standards - to date at least - only use the syntactic description mem-initializer-list which is specified as part of the parsing rules. The description is in a section entitled "Initializing bases and members" in all versions of the C++ standard dated 1998 and later. The section number does change (e.g. it's 12.6.2 in C++98, and 15.6.2 in C++17).

There is no english language description in the standard. Conventionally, people therefore use whatever wording that they consider represents the concept.

Personally, I use the term "initialiser list" since I am an english speaker in a country with accepted language more influenced by the United Kingdom than the United States.

like image 25
Peter Avatar answered Oct 27 '22 00:10

Peter