Skip to content

Commit 5bb741f

Browse files
authored
Clarify the language around zero initialization. (#157)
* Clarify the language around zero initialization. Signed-off-by: Chris Lalancette <[email protected]> * Fix up feedback from review. Signed-off-by: Chris Lalancette <[email protected]> * More PR feedback. Signed-off-by: Chris Lalancette <[email protected]>
1 parent 89ccbaf commit 5bb741f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

articles/112_generated_interfaces_cpp.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,19 @@ All other constants are declared as `static const` members in the struct and the
103103

104104
### Constructors
105105

106-
The *default constructor* initializes all members with their default value; if a field doesn't have a default value, then the field is [value-initialized](http://en.cppreference.com/w/cpp/language/value_initialization).
106+
In the following discussion, "member" refers to the class member in the C++ class while "field" refers to the field definition in the IDL file.
107+
108+
The *default constructor* initializes all members with their default value; if a field doesn't have a default value, then the member is [value-initialized](http://en.cppreference.com/w/cpp/language/value_initialization).
107109
In some cases this may not be desirable, since these fields will often be immediately overwritten with user-provided values.
108110
Therefore, the constructor takes an optional directive of type `rosidl_generator_cpp::MessageInitialization` to control how initialization is done:
109111

110-
- `MessageInitialization::ALL` - Initialize all members with their default value; if a field doesn't have a default value, then the field is [value-initialized](http://en.cppreference.com/w/cpp/language/value_initialization)
112+
- `MessageInitialization::ALL` - Initialize each member with the field's default value; if a field doesn't have a default value, then the member is [value-initialized](http://en.cppreference.com/w/cpp/language/value_initialization)
111113
- The safest option, and also the default (used if not passing any argument to the constructor).
112-
- `MessageInitialization::SKIP` - Don't initialize any members; it is the user's responsibility to ensure that all fields get initialized with some value, otherwise undefined behavior may result
114+
- `MessageInitialization::SKIP` - Don't initialize any members; it is the user's responsibility to ensure that all members get initialized with some value, otherwise undefined behavior may result
113115
- Used for maximum performance if the user is setting all of the members themselves.
114-
- `MessageInitialization::ZERO` - Zero initialize all members; this differs from `MessageInitialization::ALL` in that all members will be [value-initialized](http://en.cppreference.com/w/cpp/language/value_initialization), and default values from the message definition will be ignored
116+
- `MessageInitialization::ZERO` - Zero initialize all members; all members will be [value-initialized](http://en.cppreference.com/w/cpp/language/value_initialization) ([dynamic size](interface_definition.html#arrays-with-dynamic-size) or [upper boundary](interface_definition.html#upper-boundaries) arrays will have zero elements), and default values from the message definition will be ignored
115117
- Used when the user doesn't want the overhead of initializing potentially complex or large default values, but still wants to ensure that all variables are properly initialized.
116-
- `MessageInitialization::DEFAULTS_ONLY` - Default initialize only fields that have default values assigned to individual members; all other fields will be left uninitialized
118+
- `MessageInitialization::DEFAULTS_ONLY` - Initialize only members that have field default values; all other members will be left uninitialized
117119
- Minimal initialization which ensures that existing code has correctly initialized members when a new field with a default value is added to the IDL later.
118120

119121
Optionally the constructor can be invoked with an allocator.

0 commit comments

Comments
 (0)