Search code examples
c++c++11list-initialization

Curly brackets initialization without any value


Is it ok to write something like this

typedef unsigned long DWORD;
DWORD nBytesRead = {};

Will this variable contains 0 after this expression?


Solution

  • 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()