Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do curly braces after a struct variable member mean?

During some maintenance (Valgrind'ing) I came across this code:

#pragma pack(push, 1)
struct somename
{
  uint16_t a{};
  uint16_t b{};
  uint32_t  c{};
};
#pragma pack(pop)

I would expect that the {} tell the compiler to always initialize the values to 0 (when allocating using new, or using a stack variable), but I cannot find any examples or documentation on that. Am I correct in this assumption? If not:

What do the curly braces {} after a struct member variable mean?

like image 204
Gizmo Avatar asked Apr 30 '20 10:04

Gizmo


People also ask

What do curly braces mean in code?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

What are curly braces and used for?

{} (curly braces)Define the beginning and end of functions blocks and statement blocks such as the for and if structures. Curly braces are also used for defining initial values in array declarations.

Is it necessary to use curly braces after the for statement?

If the number of statements following the for/if is single you don't have to use curly braces. But if the number of statements is more than one, then you need to use curly braces.

What do curly brackets mean in Arduino?

After all, the same curly braces replace the RETURN statement in a subroutine (function), the ENDIF statement in a conditional and the NEXT statement in a FOR loop. Unbalanced braces can often lead to cryptic, impenetrable compiler errors that can sometimes be hard to track down in a large program.


1 Answers

This is default member initializer (since C++11).

(emphasis mine)

Through a default member initializer, which is a brace or equals initializer included in the member declaration and is used if the member is omitted from the member initializer list of a constructor.

If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored for that constructor.

As the effect, the data members a, b and c are value-initialized (zero-initialized for built-in types) to 0.

like image 169
songyuanyao Avatar answered Oct 09 '22 12:10

songyuanyao