Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is value initialization so named?

It's really unclear to me why anyone would name a particular form of initialization "value initialization". It sounds as though it's initializing the object by giving it a value... but that's what initialization does in general, and the name doesn't tell you anything about which value it's going to use for the initialization.

like image 821
Brian Bi Avatar asked Sep 03 '14 03:09

Brian Bi


People also ask

What is the difference between Default initialization and value initialization?

Again, in your case this just means that all members are value-initialized. Value initialization for fundamental types means zero-initialization, which in turn means that the variables are initialized to zero (which all fundamental types have). For objects of class type, both default- and value-initialization invoke the default constructor.

How to value initialize a named variable in C++11?

The way to value-initialize a named variable before C++11 was T object = T();, which value-initializes a temporary and then copy-initializes the object: most compilers optimize out the copy in this case. References cannot be value-initialized. As described in functional cast, the syntax T() (1) is prohibited for arrays, while T{} (5) is allowed.

What are the effects of value initialization in C++?

The effects of value initialization are: 1) if T is a class type with at least one user-provided constructor of any kind, the default constructor is called; 2) if T is a non-union class type without any user-provided constructors, every non-static data member and base-class component of T is value-initialized;

What is a zero initialized variable in C++?

A declared variable can be Zero Initialized, Value Initialized or Default Initialized. The C++03 Standard 8.5/5 aptly defines each: To zero-initialize an object of type T means: — if T is a reference type, no initialization is performed.


1 Answers

The Boost value_init write-up provide a rather detailed history of value initialization it ended up in the standard from defect report 178: More on value-initialization and it seems like the term originated from defect report 35: Definition of default-initialization. Although none of these documents really provide a proper origin for the term it does provide some good ideas, it says:

The first Technical Corrigendum for the C++ Standard (TC1), whose draft was released to the public in November 2001, introduced Core Issue 178 (among many other issues, of course).

That issue introduced the new concept of value-initialization (it also fixed the wording for zero-initialization). Informally, value-initialization is similar to default-initialization with the exception that in some cases non-static data members and base class sub-objects are also value-initialized. The difference is that an object that is value-initialized won't have (or at least is less likely to have) indeterminate values for data members and base class sub-objects; unlike the case of an object default constructed. (see Core Issue 178 for a normative description).

In order to specify value-initialization of an object we need to use the empty-set initializer: ().

and value initialization is less likely to leave an object with an indeterminate value versus default-initalization.

like image 68
Shafik Yaghmour Avatar answered Oct 12 '22 01:10

Shafik Yaghmour