Skip to content

Commit 596bf8d

Browse files
authored
Update the CPP article to talk about initialization. (#136)
* Update the CPP article to talk about initialization. Signed-off-by: Chris Lalancette <[email protected]> * runtime -> generator. This reflects the current state of the code. Signed-off-by: Chris Lalancette <[email protected]>
1 parent 631cf6b commit 596bf8d

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

articles/112_generated_interfaces_cpp.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,20 @@ All other constants are declared as `static const` members in the struct and the
103103

104104
### Constructors
105105

106-
Currently the *default constructor* does no initialization of member fields.
107-
This means that the user is responsible for ensuring that all fields are initialized before use, otherwise undefined behavior may result.
108-
In a future release, the *default constructor* will initialize all fields by default.
109-
Optionally the constructor can be invoked with an allocator as discussed [above](#messages).
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).
107+
In some cases this may not be desirable, since these fields will often be immediately overwritten with user-provided values.
108+
Therefore, the constructor takes an optional directive of type `rosidl_generator_cpp::MessageInitialization` to control how initialization is done:
109+
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)
111+
- 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
113+
- 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
115+
- 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
117+
- 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.
118+
119+
Optionally the constructor can be invoked with an allocator.
110120

111121
The struct has no constructor with positional arguments for the members.
112122
The short reason for this is that if code would rely on positional arguments to construct data objects changing a message definition would break existing code in subtle ways.

0 commit comments

Comments
 (0)