Curly brackets initialization without any value
Asked Answered
M

2

6

Is it ok to write something like this

typedef unsigned long DWORD;
DWORD nBytesRead = {};

Will this variable contains 0 after this expression?

Medrek answered 18/9, 2015 at 9:40 Comment(4)
what prevents you from trying it out?Floreneflorentia
@tobi303 I want to know what the standard says about this situation, not some specific implementationMedrek
then you probably should ask "Is this variable supposed to contain 0 after the expression according to the standard" because the compilers are more friendly than the standard dictates in many casesFloreneflorentia
yes, and this is important because it allows you to write types that do nothing in their default constructors (so that they can be cheaply put into arrays), but which you can also zero-initialize when necessary.Faena
H
6

Yes, it's legal. The standard says (5.17.9):

A braced-init-list may appear on the right-hand side of an assignment to a scalar, in which case the initializer list shall have at most a single element. The meaning of x={v} , where T is the scalar type of the expression x , is that of x=T(v) except that no narrowing conversion ( 8.5.4 ) is allowed. The meaning of x={} is x=T()

Hopple answered 18/9, 2015 at 9:44 Comment(2)
This is not the right section to quote. Assignment is not the same as initialization.Hydrophilic
This is neither assignment, nor does the answer address what value nBytesRead would have. -1.Emma
E
7

Yes it's okay and you are guaranteed that nBytesRead will contain the value zero. You are copy-initializing nBytesRead with an empty initializer list, which for a non-class type means that you are zero-initializing it. Zero-initialization means precisely what you think it means.


What you are doing is called list-copy-initialization. From [dcl.init]:

The initialization that occurs in the = form of a brace-or-equal-initializer or [...] is called copy-initialization.

From [dcl.init.list]:

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.

Where:

List-initialization of an object or reference of type T is defined as follows:
— If T is a class type and [...]
— Otherwise, if T is a character array and [...]
— Otherwise, if T is an aggregate, [...]
— Otherwise, if the initializer list has no elements and T is a class type [...]
— Otherwise, if T is a specialization of std::initializer_list, [...]
— Otherwise, if T is a class type, [...]
— Otherwise, if the initializer list has a single element [...]
— Otherwise, if T is a reference type, [...]
— Otherwise, if the initializer list has no elements, the object is value-initialized.

Value-initialization, for a non-class type, means [dcl.init]:

To value-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type with either no default constructor [...]
— if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor [...]
— if T is an array type, [...]
otherwise, the object is zero-initialized.

Zero-initialization means, [dcl.init]:

To zero-initialize an object or reference of type T means:
— if T is a scalar type (3.9), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T

Emma answered 18/9, 2015 at 12:33 Comment(0)
H
6

Yes, it's legal. The standard says (5.17.9):

A braced-init-list may appear on the right-hand side of an assignment to a scalar, in which case the initializer list shall have at most a single element. The meaning of x={v} , where T is the scalar type of the expression x , is that of x=T(v) except that no narrowing conversion ( 8.5.4 ) is allowed. The meaning of x={} is x=T()

Hopple answered 18/9, 2015 at 9:44 Comment(2)
This is not the right section to quote. Assignment is not the same as initialization.Hydrophilic
This is neither assignment, nor does the answer address what value nBytesRead would have. -1.Emma

© 2022 - 2024 — McMap. All rights reserved.