C++ struct default initializer declarations, what is best?

1 day ago 3
ARTICLE AD BOX

tl;dr what is safest way to declare a default initializer for some "complex" field like a std::vector<std::string> ?

Given the following struct declaration

export struct MyConfig { std::vector<std::string> MyField; };

What is the best way to declare a "default" instance of a MyConfig?

use specific type

This will compile

MyConfig config { .MyField = std::vector<std::string>() };

use unspecific type

This will compile

MyConfig config { .MyField = {} };

Is there a difference in these instantiations? If so, which is recommended?


This relates to avoiding C++ warning warning: missing field 'MyField' initializer [-Wmissing-designated-field-initializers]. Related to Question Why is the compiler throwing this warning: "missing initializer"? Isn't the structure initialized?

Read Entire Article