{"id":5936,"date":"2020-07-02T12:47:16","date_gmt":"2020-07-02T12:47:16","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/designated-initializers\/"},"modified":"2023-06-26T09:48:10","modified_gmt":"2023-06-26T09:48:10","slug":"designated-initializers","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/designated-initializers\/","title":{"rendered":"Designated Initializers"},"content":{"rendered":"<p>Designated initialization is an extension of aggregate initialization and empowers you to directly initialize the members of a class type using their names.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5199\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/02\/TimelineCpp20.png\" alt=\"TimelineCpp20\" width=\"650\" height=\"242\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>Designated initialization is a special case of aggregate initialization. Writing about designated initialization means, therefore, writing about aggregate initialization.<\/p>\n<h2>Aggregate Initialization<\/h2>\n<p>First: what is an aggregate? Aggregates are arrays and class types. A class type is a <span style=\"font-family: courier new, courier;\">class<\/span>, a <span style=\"font-family: courier new, courier;\">struct<\/span>, or a <span style=\"font-family: courier new, courier;\">union<\/span>.<\/p>\n<p>With C++20, the following condition must hold class types:<\/p>\n<ul>\n<li>no private or protected non-static data members<\/li>\n<li>no user-declared or inherited constructors<\/li>\n<li>no <span class=\"t-rev-inl t-since-cxx17\">virtual, private, or protected <span class=\"t-mark-rev t-since-cxx17\"><\/span><\/span>base classes<\/li>\n<li>no virtual member functions<\/li>\n<\/ul>\n<p>The next program exemplifies aggregate initialization.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ aggregateInitialization.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Point2D{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x; \r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Point3D<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> z;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    Point2D point2D{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>};        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    Point3D point3D{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>, <span style=\"color: #ff6600;\">3<\/span>};     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"point2D: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point2D.x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point2D.y <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"point3D: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3D.x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3D.y <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3D.z <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>(1) and (2) directly initialize the aggregates using curly braces. The sequence of the initializers in the curly braces has to match the declaration order of the members.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5932\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/aggregateInitialization.png\" alt=\"aggregateInitialization\" width=\"450\" height=\"180\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/aggregateInitialization.png 1356w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/aggregateInitialization-300x120.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/aggregateInitialization-1024x409.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/aggregateInitialization-768x307.png 768w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>Based on aggregate initialization in C++11, we get designed initializers in C++20. So far, only the Microsoft compiler support designated initializers completely.<\/p>\n<\/p>\n<h2>Designated Initializers<\/h2>\n<p>Designated initializers enable it to initialize members of a class type using their name directly. For a union, only one initializer can be provided. As for aggregate initialization, the sequence of initializers in the curly braces has to match the declaration order of the members.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ designatedInitializer.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Point2D{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Point3D<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> z;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    Point2D point2D{.x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>, .y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>};          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    Point3D point3D{.x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>, .y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>, .z <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">3<\/span>};  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"point2D: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point2D.x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point2D.y <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"point3D: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3D.x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3D.y <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3D.z <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>(1) and (2) use designated initializers to initialize the aggregates. The initializers, such as <span style=\"font-family: courier new, courier;\">.x<\/span> or <span style=\"font-family: courier new, courier;\">.y<\/span> are often called designators.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5933\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializer.png\" alt=\"designatedInitializer\" width=\"450\" height=\"190\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializer.png 1347w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializer-300x127.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializer-1024x433.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializer-768x324.png 768w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>The members of the aggregate can already have a default value. This default value is used when the initializer is missing. This does not hold for a union.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ designatedInitializersDefaults.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Point3D<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>; \r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> z <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">needPoint<\/span>(Point3D p) {\r\n     std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"p: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> p.x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> p.y <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> p.z <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    Point3D point1{.x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>, .y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>, .z <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>};     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"point1: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point1.x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point1.y <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point1.z <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    Point3D point2;                             <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"point2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point2.x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point2.y <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point2.z <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    Point3D point3{.x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>, .z <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">20<\/span>};            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"point3: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3.x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3.y <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3.z <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ Point3D point4{.z = 20, .y = 1}; ERROR   \/\/ (4) <\/span>\r\n    \r\n    needPoint({.x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>});                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>(1) initializes all members,&nbsp; but (2) does not provide a value for member <span style=\"font-family: courier new, courier;\">x<\/span>. Consequently, <span style=\"font-family: courier new, courier;\">x <\/span>is not initialized. It is fine if you only initialize the members who don&#8217;t have a default value such as in (3) or (5). The expression (4) would not compile because <span style=\"font-family: courier new, courier;\">z<\/span> and <span style=\"font-family: courier new, courier;\">y<\/span> are in the wrong order.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5934\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerDefaults.png\" alt=\"designatedInitializerDefaults\" width=\"450\" height=\"201\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerDefaults.png 1551w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerDefaults-300x134.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerDefaults-1024x458.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerDefaults-768x343.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerDefaults-1536x686.png 1536w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>Designated initializers detect narrowing conversion. Narrowing conversion is a conversion of a value, including the loss of its precision.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ designatedInitializerNarrowingConversion.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Point2D{\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Point3D<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> z;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    Point2D point2D{.x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>, .y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2.5<\/span>};            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    Point3D point3D{.x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>, .y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>, .z <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">3.5f<\/span>};   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"point2D: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point2D.x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point2D.y <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"point3D: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3D.x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3D.y <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> point3D.z <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>(1) and (2) produce a compile-time error because the initialization <span style=\"font-family: courier new, courier;\">.y = 2.5<\/span> and <span style=\"font-family: courier new, courier;\">.z = 3.5f<\/span> would cause a narrowing conversion to in.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5935\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerNarrowingConversion.png\" alt=\"designatedInitializerNarrowingConversion\" width=\"650\" height=\"182\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerNarrowingConversion.png 3032w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerNarrowingConversion-300x84.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerNarrowingConversion-1024x287.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerNarrowingConversion-768x215.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerNarrowingConversion-1536x430.png 1536w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/designatedInitializerNarrowingConversion-2048x573.png 2048w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Interestingly, designated initializers in C behave differently to designated initializers in C++.<\/p>\n<h3>Differences between C and C++<\/h3>\n<p>C supports use cases that are not supported in C++. C allows<\/p>\n<ul>\n<li>to initialize the members of the aggregate out-of-order<\/li>\n<li>to initialize the members of a nested aggregate<\/li>\n<li>to mix designated initializers and regular initializers<\/li>\n<li>designated initialization of arrays<\/li>\n<\/ul>\n<p>Proposal<a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2017\/p0329r4.pdf\"> P0329R4<\/a> provides self-explanatory examples for these use cases:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">struct<\/span> A { <span style=\"color: #007788; font-weight: bold;\">int<\/span> x, y; };\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> B { <span style=\"color: #006699; font-weight: bold;\">struct<\/span> A a; };\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> A a <span style=\"color: #555555;\">=<\/span> {.y <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>, .x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">2<\/span>}; <span style=\"color: #0099ff; font-style: italic;\">\/\/ valid C, invalid C++ (out of order)<\/span>\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> arr[<span style=\"color: #ff6600;\">3<\/span>] <span style=\"color: #555555;\">=<\/span> {[<span style=\"color: #ff6600;\">1<\/span>] <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">5<\/span>};        <span style=\"color: #0099ff; font-style: italic;\">\/\/ valid C, invalid C++ (array)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> B b <span style=\"color: #555555;\">=<\/span> {.a.x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>};       <span style=\"color: #0099ff; font-style: italic;\">\/\/ valid C, invalid C++ (nested)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> A a <span style=\"color: #555555;\">=<\/span> {.x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>};      <span style=\"color: #0099ff; font-style: italic;\">\/\/ valid C, invalid C++ (mixed)<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The rationale for this difference between C and C++ is also part of the proposal: &#8220;<em>In C++, members are destroyed in reverse construction order and the elements of an initializer list are evaluated in lexical order, so field initializers must be specified in the order. Array designators conflict with \u200blambda-expression\u200b syntax. Nested designators are seldom used<\/em>.&#8221; The paper argues that only out-of-order initialization of aggregate is commonly used.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Wow! With C++98, we got <span style=\"font-family: 'courier new', courier;\">const<\/span>, with C++11 <span style=\"font-family: 'courier new', courier;\">constexpr<\/span>, and with C++20 <span style=\"font-family: 'courier new', courier;\">consteval<\/span> and <span style=\"font-family: 'courier new', courier;\">constinit<\/span>.&nbsp; In my <a href=\"https:\/\/www.modernescpp.com\/index.php\/designated-initializers\">next post,<\/a> I will write about the new C++20 specifiers&nbsp;<span style=\"font-family: 'courier new', courier;\">consteval<\/span> and <span style=\"font-family: 'courier new', courier;\">constinit<\/span> and their differences from <span style=\"font-family: 'courier new', courier;\">const<\/span> and <span style=\"font-family: 'courier new', courier;\">constexpr<\/span>.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<div id=\"simple-translate\">&nbsp;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Designated initialization is an extension of aggregate initialization and empowers you to directly initialize the members of a class type using their names.<\/p>\n","protected":false},"author":21,"featured_media":5199,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[460],"class_list":["post-5936","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-initialization"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5936"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5936\/revisions"}],"predecessor-version":[{"id":6735,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5936\/revisions\/6735"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5199"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}