Namespaces
Variants
Views
Actions

Talk:cpp/language/initialization

From cppreference.com

Is

   T t{};

value-initialization? I'm not sure. C++11's 8.5/16 says

If the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized (8.5.4).

8.5.4 describes list-initialization:

List-initialization is initialization of an object or reference from a braced-init-list. Such an initializer is called an initializer list, and the comma-separated initializer-clauses of the list are called the elements of the initializer list. An initializer list may be empty. List-initialization can occur in direct-initialization or copy-initialization contexts; list-initialization in a direct-initialization context is called direct-list-initialization and list-initialization in a copy-initialization context is called copy-list-initialization.

So for what I understand,

T t{};

is a list-initialization.

As for value-initialization, 8.5/10 says

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

8.5/16 repeats that in the forth bullet-point.

So, I think only initialization via () is termed value-initialization. Of course,

   T t();

doesn't create an object or a variable but declares a function; but it's possible to instantiate an object with () in other contexts (with a new expression, explicit type conversion, and initializing bases and members).

... But then, 8.5/15 calls

   T x(a);
   T x{a};

direct initialization, which, for the braces form, I find confusing, because just a paragraph later it states that's list-initialization...



Edit: 8.5.4 says list-initialization can occur in direct-initialization. Wow. This is a complex business.


--wilhelmtell (talk) 00:25, 20 April 2015 (PDT)




I've replaced the value-initialization example

   std::string s{};

with

   process(std::string());

because I'm not sure the former is a value-initialization, but I am sure the latter is, by 8.5/10 of the C++11 standard. I don't have access to the C++14 standard to check that there...

--wilhelmtell (talk) 00:51, 20 April 2015 (PDT)



Yes, T x{}; value-initializes x. The syntactic form is direct list-initialization, but the semantic action of that form is value initialization ("if the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized" -- 8.5.4[dcl.init.list]/3.4 (same wording in C++11, different bullet point)). Regarding your edit, if process takes its argument by value, this is an example of copy-init (from a value-init'd temporary), otherwise ref-init (from a value-init temporary). I've put {} back, although perhaps a micro-example showing round parens could be a useful addition. Something like "string s{}; or new int();" --Cubbi (talk) 06:06, 20 April 2015 (PDT)

This text is either wrong or confusing:

                 // may be statically initialized to 0.0 or
                 // dynamically initialized to 0.0 if d1 is dynamically initialized, or
                 // dynamically initialized to 1.0 if d1 is dynamically initialized

Does it mean to say "dynamically initialized to 0.0 or 1.0 if d1 is dynamically initialized" or is it a copy-paste error. I'm guessing the later but not certain. Mliberty (talk) 13:02, 16 February 2016 (PST)

indeed, this was a badly done copy from the standard (where it says "1.0 otherwise"). --Cubbi (talk) 14:09, 16 February 2016 (PST)