value_init.htm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type"
  4. content="text/html; charset=iso-8859-1">
  5. <title>value_initialized</title>
  6. </head>
  7. <body vlink="#800080" link="#0000ff" text="#000000" bgcolor="#ffffff">
  8. <h2><img src="../../boost.png" width="276" height="86">
  9. Header &lt;<a href="../../boost/utility/value_init.hpp">boost/utility/value_init.hpp</a>&gt;
  10. </h2>
  11. <h2>Contents</h2>
  12. <dl>
  13. <dt><a href="#rationale">Rationale</a></dt>
  14. <dt><a href="#intro">Introduction</a></dt>
  15. <dt><a href="#details">Details</a></dt>
  16. </dl>
  17. <ul>
  18. <li><a href="#valueinit">value-initialization</a></li>
  19. <li><a href="#valueinitsyn">value-initialization syntax</a></li>
  20. <li><a href="#compiler_issues">compiler issues</a></li>
  21. </ul>
  22. <dl class="page-index">
  23. <dt><a href="#types">Types and objects</a></dt>
  24. </dl>
  25. <ul>
  26. <li><a href="#val_init"><code>template class value_initialized&lt;T&gt;</code></a></li>
  27. <li><a href="#initialized"><code>template class initialized&lt;T&gt;</code></a></li>
  28. <li><a href="#initialized_value"><code>initialized_value</code></a></li>
  29. </ul>
  30. <a href="#acknowledgements">Acknowledgements</a><br>
  31. <br>
  32. <hr>
  33. <h2><a name="rationale"></a>Rationale</h2>
  34. <p>Constructing and initializing objects in a generic way is difficult in
  35. C++. The problem is that there are several different rules that apply
  36. for initialization. Depending on the type, the value of a newly constructed
  37. object can be zero-initialized (logically 0), default-constructed (using
  38. the default constructor), or indeterminate. When writing generic code,
  39. this problem must be addressed. The template <code>value_initialized</code> provides
  40. a solution with consistent syntax for value initialization of scalar,
  41. union and class types.
  42. Moreover, <code>value_initialized</code> offers a workaround to various
  43. compiler issues regarding value-initialization.
  44. Furthermore, a <code>const</code> object, <code>initialized_value</code> is provided,
  45. to avoid repeating the type name when retrieving the value from a
  46. <code>value_initialized&lt;T&gt;</code> object.
  47. <br>
  48. </p>
  49. <h2><a name="intro"></a>Introduction</h2>
  50. <p>
  51. There are various ways to initialize a variable, in C++. The following
  52. declarations all <em>may</em> have a local variable initialized to its default
  53. value:
  54. <pre>
  55. T1 var1;
  56. T2 var2 = 0;
  57. T3 var3 = {};
  58. T4 var4 = T4();
  59. </pre>
  60. Unfortunately, whether or not any of those declarations correctly
  61. initialize the variable very much depends on its type. The first
  62. declaration is valid for any <a href="http://www.sgi.com/tech/stl/DefaultConstructible.html">
  63. DefaultConstructible</a> type (by definition).
  64. However, it does not always do an initialization!
  65. It correctly initializes the variable when it's an instance of a
  66. class, and the author of the class has provided a proper default
  67. constructor. On the other hand, the value of <code>var1</code> is <em>indeterminate</em> when
  68. its type is an arithmetic type, like <code>int</code>, <code>float</code>, or <code>char</code>.
  69. An arithmetic variable
  70. is of course initialized properly by the second declaration, <code>T2
  71. var2 = 0</code>. But this initialization form usually won't work for a
  72. class type (unless the class was especially written to support being
  73. initialized that way). The third form, <code>T3 var3 = {}</code>
  74. initializes an aggregate, typically a "C-style" <code>struct</code> or a "C-style" array.
  75. However, the syntax is not allowed for a class that has an explicitly declared
  76. constructor. (But watch out for an upcoming C++ language change,
  77. by Bjarne Stroustrup et al [<a href="#references">1</a>]!)
  78. The fourth form is the most generic form of them, as it
  79. can be used to initialize arithmetic types, class types, aggregates, pointers, and
  80. other types. The declaration, <code>T4 var4 = T4()</code>, should be read
  81. as follows: First a temporary object is created, by <code>T4()</code>.
  82. This object is <a href="#valueinit">value-initialized</a>. Next the temporary
  83. object is copied to the named variable, <code>var4</code>. Afterwards, the temporary
  84. is destroyed. While the copying and the destruction are likely to
  85. be optimized away, C++ still requires the type <code>T4</code> to be
  86. <a href="CopyConstructible.html">CopyConstructible</a>.
  87. (So <code>T4</code> needs to be <em>both</em> DefaultConstructible <em>and</em> CopyConstructible.)
  88. A class may not be CopyConstructible, for example because it may have a
  89. private and undefined copy constructor,
  90. or because it may be derived from <a href="utility.htm#Class_noncopyable">boost::noncopyable</a>.
  91. Scott Meyers [<a href="#references">2</a>] explains why a class would be defined like that.
  92. </p>
  93. <p>
  94. There is another, less obvious disadvantage to the fourth form, <code>T4 var4 = T4()</code>:
  95. It suffers from various <a href="#compiler_issues">compiler issues</a>, causing
  96. a variable to be left uninitialized in some compiler specific cases.
  97. </p>
  98. <p>
  99. The template <a href="#val_init"><code>value_initialized</code></a>
  100. offers a generic way to initialize
  101. an object, like <code>T4 var4 = T4()</code>, but without requiring its type
  102. to be CopyConstructible. And it offers a workaround to those compiler issues
  103. regarding value-initialization as well! It allows getting an initialized
  104. variable of any type; it <em>only</em> requires the type to be DefaultConstructible.
  105. A properly <em>value-initialized</em> object of type <code>T</code> is
  106. constructed by the following declaration:
  107. <pre>
  108. value_initialized&lt;T&gt; var;
  109. </pre>
  110. </p>
  111. <p>
  112. The template <a href="#initialized"><code>initialized</code></a>
  113. offers both value-initialization and direct-initialization.
  114. It is especially useful as a data member type, allowing the very same object
  115. to be either direct-initialized or value-initialized.
  116. </p>
  117. <p>
  118. The <code>const</code> object <a href="#initialized_value"><code>initialized_value</code></a>
  119. allows value-initializing a variable as follows:
  120. <pre>
  121. T var = initialized_value ;
  122. </pre>
  123. This form of initialization is semantically equivalent to <code>T4 var4 = T4()</code>,
  124. but robust against the aforementioned compiler issues.
  125. </p>
  126. <h2><a name="details"></a>Details</h2>
  127. <p>The C++ standard [<a href="#references">3</a>] contains the definitions
  128. of <code>zero-initialization</code> and <code>default-initialization</code>.
  129. Informally, zero-initialization means that the object is given the initial
  130. value 0 (converted to the type) and default-initialization means that
  131. POD [<a href="#references">4</a>] types are zero-initialized, while non-POD class
  132. types are initialized with their corresponding default constructors. A
  133. <i>declaration</i> can contain an <i>initializer</i>, which specifies the
  134. object's initial value. The initializer can be just '()', which states that
  135. the object shall be value-initialized (but see below). However, if a <i>declaration</i>
  136. has no <i>initializer</i> and it is of a non-<code>const</code>, non-<code>static</code>
  137. POD type, the initial value is indeterminate: <cite>(see &sect;8.5, [dcl.init], for the
  138. accurate definitions).</cite></p>
  139. <pre>int x ; // no initializer. x value is indeterminate.<br>std::string s ; // no initializer, s is default-constructed.<br><br>int y = int() ; <br>// y is initialized using copy-initialization<br>// but the temporary uses an empty set of parentheses as the initializer,<br>// so it is default-constructed.<br>// A default constructed POD type is zero-initialized,<br>// therefore, y == 0.<br><br>void foo ( std::string ) ;<br>foo ( std::string() ) ; <br>// the temporary string is default constructed <br>// as indicated by the initializer () </pre>
  140. <h3><a name="valueinit">value-initialization</a></h3>
  141. <p>The first <a
  142. href="http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html">Technical
  143. Corrigendum for the C++ Standard</a> (TC1), whose draft was released to
  144. the public in November 2001, introduced <a
  145. href="http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#178">Core
  146. Issue 178</a> (among many other issues, of course).</p>
  147. <p> That issue introduced the new concept of <code>value-initialization</code>
  148. (it also fixed the wording for zero-initialization). Informally, value-initialization
  149. is similar to default-initialization with the exception that in some cases
  150. non-static data members and base class sub-objects are also value-initialized.
  151. The difference is that an object that is value-initialized won't have
  152. (or at least is less likely to have) indeterminate values for data members
  153. and base class sub-objects; unlike the case of an object default constructed.
  154. (see Core Issue 178 for a normative description).</p>
  155. <p>In order to specify value-initialization of an object we need to use the
  156. empty-set initializer: (). </p>
  157. <p>As before, a declaration with no intializer specifies default-initialization,
  158. and a declaration with a non-empty initializer specifies copy (=xxx) or
  159. direct (xxx) initialization. </p>
  160. <pre>template&lt;class T&gt; void eat(T);<br>int x ; // indeterminate initial value.<br>std::string s; // default-initialized.<br>eat ( int() ) ; // value-initialized<br>eat ( std::string() ) ; // value-initialized</pre>
  161. <h4><a name="valueinitsyn">value-initialization</a> syntax</h4>
  162. <p>Value initialization is specified using (). However, the empty set of
  163. parentheses is not permitted by the syntax of initializers because it is
  164. parsed as the declaration of a function taking no arguments: </p>
  165. <pre>int x() ; // declares function int(*)()</pre>
  166. <p>Thus, the empty () must be put in some other initialization context.</p>
  167. <p>One alternative is to use copy-initialization syntax:</p>
  168. <pre>int x = int() ;</pre>
  169. <p>This works perfectly fine for POD types. But for non-POD class types,
  170. copy-initialization searches for a suitable constructor, which could be,
  171. for instance, the copy-constructor (it also searches for a suitable conversion
  172. sequence but this doesn't apply in this context). For an arbitrary unknown
  173. type, using this syntax may not have the value-initialization effect intended
  174. because we don't know if a copy from a default constructed object is exactly
  175. the same as a default constructed object, and the compiler is allowed (in
  176. some cases), but never required to, optimize the copy away.</p>
  177. <p>One possible generic solution is to use value-initialization of a non static
  178. data member:</p>
  179. <pre>template&lt;class T&gt; <br>struct W <br>{<br> // value-initialization of 'data' here.<br> W() : data() {}<br> T data ;<br>} ;<br>W&lt;int&gt; w ;<br>// w.data is value-initialized for any type. </pre>
  180. <p>This is the solution as it was supplied by earlier versions of the
  181. <code>value_initialized&lt;T&gt;</code> template
  182. class. Unfortunately this approach suffered from various compiler issues.</p>
  183. <h4><a name="compiler_issues">compiler issues</a> </h4>
  184. Various compilers haven't yet fully implemented value-initialization.
  185. So when an object should be <em>value-initialized</em> (according to the C++ Standard),
  186. it <em>may</em> in practice still be left uninitialized, because of those
  187. compiler issues! It's hard to make a general statement on what those issues
  188. are like, because they depend on the compiler you are using, its version number,
  189. and the type of object you would like to have value-initialized.
  190. All compilers we have tested so far support value-initialization for arithmetic types properly.
  191. However, various compilers may leave some types of <em>aggregates</em> uninitialized, when they
  192. should be value-initialized. Value-initialization of objects of a pointer-to-member type may also
  193. go wrong on various compilers.
  194. </p>
  195. <p>
  196. At the moment of writing, May 2010, the following reported issues regarding
  197. value-initialization are still there in current compiler releases:
  198. <ul>
  199. <li>
  200. <a href="https://connect.microsoft.com/VisualStudio/feedback/details/100744">
  201. Microsoft Visual Studio Feedback ID 100744, Value-initialization in new-expression</a>
  202. <br>Reported by Pavel Kuznetsov (MetaCommunications Engineering), 2005
  203. </li><li>
  204. <a href="http://connect.microsoft.com/VisualStudio/feedback/details/484295">
  205. Microsoft Visual Studio Feedback ID 484295, VC++ does not value-initialize members of derived classes without user-declared constructor</a>
  206. <br>Reported by Sylvester Hesp, 2009
  207. </li><li>
  208. <a href="https://connect.microsoft.com/VisualStudio/feedback/details/499606">
  209. Microsoft Visual Studio Feedback ID 499606, Presence of copy constructor breaks member class initialization</a>
  210. <br>Reported by Alex Vakulenko, 2009
  211. </li><li>
  212. <a href="http://qc.embarcadero.com/wc/qcmain.aspx?d=83751">
  213. Embarcadero/C++Builder Report 83751, Value-initialization: arrays should have each element value-initialized</a>
  214. <br>Reported by Niels Dekker (LKEB), 2010
  215. </li><li>
  216. <a href="http://qc.embarcadero.com/wc/qcmain.aspx?d=83851">
  217. Embarcadero/C++Builder Report 83851, Value-initialized temporary triggers internal backend error C1798</a>
  218. <br>Reported by Niels Dekker, 2010
  219. </li><li>
  220. <a href="http://qc.embarcadero.com/wc/qcmain.aspx?d=84279">
  221. Embarcadero/C++Builder Report 84279, Internal compiler error (F1004), value-initializing member function pointer by "new T()"</a>
  222. <br>Reported by Niels Dekker, 2010
  223. </li><li>
  224. Sun CR 6947016, Sun 5.10 may fail to value-initialize an object of a non-POD aggregate.
  225. <br>Reported to Steve Clamage by Niels Dekker, 2010
  226. </li><li>
  227. IBM's XL V10.1 and V11.1 may fail to value-initialize a temporary of a non-POD aggregate.
  228. <br>Reported to Michael Wong by Niels Dekker, 2010
  229. </li><li>
  230. Intel support issue 589832, Attempt to value-initialize pointer-to-member triggers internal error
  231. on Intel 11.1.
  232. <br>Reported by John Maddock, 2010
  233. </li>
  234. </ul>
  235. Note that all known GCC issues regarding value-initialization are
  236. fixed with GCC version 4.4, including
  237. <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30111">GCC Bug 30111</a>.
  238. Clang also has completely implemented value-initialization, as far as we know,
  239. now that <a href="http://llvm.org/bugs/show_bug.cgi?id=7139">Clang Bug 7139</a> is fixed.
  240. </p><p>
  241. New versions of <code>value_initialized</code>
  242. (Boost release version 1.35 or higher)
  243. offer a workaround to these issues: <code>value_initialized</code> may now clear
  244. its internal data, prior to constructing the object that it contains. It will do
  245. so for those compilers that need to have such a workaround, based on the
  246. <a href="../config/doc/html/boost_config/boost_macro_reference.html#boost_config.boost_macro_reference.macros_that_describe_defects"
  247. >compiler defect macro</a> BOOST_NO_COMPLETE_VALUE_INITIALIZATION.
  248. </p>
  249. <h2><a name="types"></a>Types and objects</h2>
  250. <h2><a name="val_init"><code>template class value_initialized&lt;T&gt;</code></a></h2>
  251. <pre>namespace boost {<br><br>template&lt;class T&gt;<br>class value_initialized<br>{
  252. <br> public :
  253. <br> value_initialized() : x() {}
  254. <br> operator T const &amp;() const { return x ; }
  255. <br> operator T&amp;() { return x ; }
  256. <br> T const &amp;data() const { return x ; }
  257. <br> T&amp; data() { return x ; }
  258. <br> void swap( value_initialized&amp; );
  259. <br>
  260. <br> private :
  261. <br> <i>unspecified</i> x ;
  262. <br>} ;
  263. <br>
  264. <br>template&lt;class T&gt;
  265. <br>T const&amp; get ( value_initialized&lt;T&gt; const&amp; x )
  266. <br>{
  267. <br> return x.data() ;
  268. <br>}
  269. <br>
  270. <br>template&lt;class T&gt;
  271. <br>T&amp; get ( value_initialized&lt;T&gt;&amp; x )
  272. <br>{
  273. <br> return x.data() ;
  274. <br>}
  275. <br>
  276. <br>template&lt;class T&gt;
  277. <br>void swap ( value_initialized&lt;T&gt;&amp; lhs, value_initialized&lt;T&gt;&amp; rhs )
  278. <br>{
  279. <br> lhs.swap(rhs) ;
  280. <br>}
  281. <br>
  282. <br>} // namespace boost
  283. <br></pre>
  284. <p>An object of this template class is a <code>T</code>-wrapper convertible
  285. to <code>'T&amp;'</code> whose wrapped object (data member of type <code>T</code>)
  286. is <a href="#valueinit">value-initialized</a> upon default-initialization
  287. of this wrapper class: </p>
  288. <pre>int zero = 0 ;<br>value_initialized&lt;int&gt; x ;<br>assert ( x == zero ) ;<br><br>std::string def ;<br>value_initialized&lt; std::string &gt; y ;<br>assert ( y == def ) ;<br></pre>
  289. <p>The purpose of this wrapper is to provide a consistent syntax for value
  290. initialization of scalar, union and class types (POD and non-POD) since
  291. the correct syntax for value initialization varies (see <a
  292. href="#valueinitsyn">value-initialization syntax</a>)</p>
  293. <p>The wrapped object can be accessed either through the conversion operator
  294. <code>T&amp;</code>, the member function <code>data()</code>, or the
  295. non-member function <code>get()</code>: </p>
  296. <pre>void watch(int);<br>value_initialized&lt;int&gt; x;
  297. <br><br>watch(x) ; // operator T&amp; used.<br>watch(x.data());<br>watch( get(x) ) // function get() used</pre>
  298. <p>Both <code>const</code> and non-<code>const</code> objects can be wrapped.
  299. Mutable objects can be modified directly from within the wrapper but constant
  300. objects cannot:</p>
  301. <p>When <code>T</code> is a <em>Swappable</em> type, <code>value_initialized&lt;T&gt;</code>
  302. is swappable as well, by calling its <code>swap</code> member function
  303. as well as by calling <code>boost::swap</code>.</p>
  304. <pre>value_initialized&lt;int&gt; x ; <br>static_cast&lt;int&amp;&gt;(x) = 1 ; // OK<br>get(x) = 1 ; // OK
  305. <br><br>value_initialized&lt;int const&gt; y ; <br>static_cast&lt;int&amp;&gt;(y) = 1 ; // ERROR: cannot cast to int&amp;<br>static_cast&lt;int const&amp;&gt;(y) = 1 ; // ERROR: cannot modify a const value<br>get(y) = 1 ; // ERROR: cannot modify a const value</pre>
  306. <h3>Warning:</h3>
  307. <p>The <code>value_initialized</code> implementation of Boost version 1.40.0 and older
  308. allowed <i>non-const</i> access to the wrapped object, from a constant wrapper,
  309. both by its conversion operator and its <code>data()</code> member function. For example:</p>
  310. <pre>value_initialized&lt;int&gt; const x_c ;<br>int&amp; xr = x_c ; // OK, conversion to int&amp; available even though x_c is itself const.
  311. <br>xr = 2 ; </pre>
  312. <p>The reason for this obscure behavior was that some compilers
  313. didn't accept the following valid code:</p>
  314. <pre>struct X<br>{<br> operator int&amp;() ;<br> operator int const&amp;() const ; <br>};<br>X x ;<br>(x == 1 ) ; // ERROR HERE!</pre>
  315. <p>The current version of <code>value_initialized</code> no longer has this obscure behavior.
  316. As compilers nowadays widely support overloading the conversion operator by having a <code>const</code> and a <code>non-const</code> version, we have decided to fix the issue accordingly. So the current version supports the idea of logical constness.
  317. <br>
  318. </p>
  319. <h3>Recommended practice: The non-member get() idiom</h3>
  320. <p>The obscure behavior of being able to modify a non-<code>const</code>
  321. wrapped object from within a constant wrapper (as was supported by previous
  322. versions of <code>value_initialized</code>)
  323. can be avoided if access to
  324. the wrapped object is always performed with the <code>get()</code> idiom:</p>
  325. <pre>value_initialized&lt;int&gt; x ;<br>get(x) = 1 ; // OK<br><br>value_initialized&lt;int const&gt; cx ;<br>get(x) = 1 ; // ERROR: Cannot modify a const object<br><br>value_initialized&lt;int&gt; const x_c ;<br>get(x_c) = 1 ; // ERROR: Cannot modify a const object<br><br>value_initialized&lt;int const&gt; const cx_c ;<br>get(cx_c) = 1 ; // ERROR: Cannot modify a const object<br></pre>
  326. <h2><a name="initialized"><code>template class initialized&lt;T&gt;</code></a></h2>
  327. <pre>namespace boost {<br><br>template&lt;class T&gt;<br>class initialized<br>{
  328. <br> public :
  329. <br> initialized() : x() {}
  330. <br> explicit initialized(T const &amp; arg) : x(arg) {}
  331. <br> operator T const &amp;() const;
  332. <br> operator T&amp;();
  333. <br> T const &amp;data() const;
  334. <br> T&amp; data();
  335. <br> void swap( initialized&amp; );
  336. <br>
  337. <br> private :
  338. <br> <i>unspecified</i> x ;
  339. <br>} ;
  340. <br>
  341. <br>template&lt;class T&gt;
  342. <br>T const&amp; get ( initialized&lt;T&gt; const&amp; x );
  343. <br>
  344. <br>template&lt;class T&gt;
  345. <br>T&amp; get ( initialized&lt;T&gt;&amp; x );
  346. <br>
  347. <br>template&lt;class T&gt;
  348. <br>void swap ( initialized&lt;T&gt;&amp; lhs, initialized&lt;T&gt;&amp; rhs );
  349. <br>
  350. <br>} // namespace boost
  351. <br></pre>
  352. The template class <code>boost::initialized&lt;T&gt;</code> supports both value-initialization
  353. and direct-initialization, so its interface is a superset of the interface
  354. of <code>value_initialized&lt;T&gt;</code>: Its default-constructor
  355. value-initializes the wrapped object just like the default-constructor of
  356. <code>value_initialized&lt;T&gt;</code>, but <code>boost::initialized&lt;T&gt;</code>
  357. also offers an extra <code>explicit</code>
  358. constructor, which direct-initializes the wrapped object by the specified value.
  359. <p>
  360. <code>initialized&lt;T&gt;</code> is especially useful when the wrapped
  361. object must be either value-initialized or direct-initialized, depending on
  362. runtime conditions. For example, <code>initialized&lt;T&gt;</code> could
  363. hold the value of a data member that may be value-initialized by some
  364. constructors, and direct-initialized by others.
  365. On the other hand, if it is known beforehand that the
  366. object must <i>always</i> be value-initialized, <code>value_initialized&lt;T&gt;</code>
  367. may be preferable. And if the object must always be
  368. direct-initialized, none of the two wrappers really needs to be used.
  369. </p>
  370. <h2><a name="initialized_value"><code>initialized_value</code></a></h2>
  371. <pre>
  372. namespace boost {
  373. class initialized_value_t
  374. {
  375. public :
  376. template &lt;class T&gt; operator T() const ;
  377. };
  378. initialized_value_t const initialized_value = {} ;
  379. } // namespace boost
  380. </pre>
  381. <code>initialized_value</code> provides a convenient way to get
  382. an initialized value: its conversion operator provides an appropriate
  383. <em>value-initialized</em> object for any CopyConstructible type.
  384. Suppose you need to have an initialized variable of type <code>T</code>.
  385. You could do it as follows:
  386. <pre>
  387. T var = T();
  388. </pre>
  389. But as mentioned before, this form suffers from various compiler issues.
  390. The template <code>value_initialized</code> offers a workaround:
  391. <pre>
  392. T var = get( value_initialized&lt;T&gt;() );
  393. </pre>
  394. Unfortunately both forms repeat the type name, which
  395. is rather short now (<code>T</code>), but could of course be
  396. more like <code>Namespace::Template&lt;Arg&gt;::Type</code>.
  397. Instead, one could use <code>initialized_value</code> as follows:
  398. <pre>
  399. T var = initialized_value ;
  400. </pre>
  401. <h3><a name="references">References</a></h3>
  402. [1] Bjarne Stroustrup, Gabriel Dos Reis, and J. Stephen Adamczyk wrote
  403. various papers, proposing to extend the support for brace-enclosed <em>initializer lists</em>
  404. in the next version of C++.
  405. This would allow a variable <code>var</code> of any DefaultConstructible type
  406. <code>T</code> to be <em>value-initialized</em> by doing <code>T var = {}</code>.
  407. The papers are listed at Bjarne's web page,
  408. <a href="http://www.research.att.com/~bs/WG21.html">My C++ Standards committee papers</a> <br>
  409. [2] Scott Meyers, Effective C++, Third Edition, item 6,
  410. <em>Explicitly disallow the use of compiler-generated functions you do not want</em>,
  411. <a href="http://www.aristeia.com/books.html">Scott Meyers: Books and CDs</a> <br>
  412. [3] The C++ Standard, Second edition (2003), ISO/IEC 14882:2003 <br>
  413. [4] POD stands for "Plain Old Data"
  414. <h3><a name="acknowledgements"></a>Acknowledgements</h3>
  415. value_initialized was developed by Fernando Cacciola, with help and
  416. suggestions from David Abrahams and Darin Adler.<br>
  417. Special thanks to Bj&ouml;rn Karlsson who carefully edited and completed this documentation.
  418. <p>value_initialized was reimplemented by Fernando Cacciola and Niels Dekker
  419. for Boost release version 1.35 (2008), offering a workaround to various compiler issues.
  420. </p>
  421. <p><code>boost::initialized</code> was very much inspired by feedback from Edward Diener and
  422. Jeffrey Hellrung.
  423. </p>
  424. <p>initialized_value was written by Niels Dekker, and added to Boost release version 1.36 (2008).
  425. </p>
  426. <p>Developed by <a href="mailto:fernando_cacciola@hotmail.com">Fernando Cacciola</a>,
  427. the latest version of this file can be found at <a
  428. href="http://www.boost.org">www.boost.org</a>.
  429. </p>
  430. <hr>
  431. <p>Revised 30 May 2010</p>
  432. <p>&copy; Copyright Fernando Cacciola, 2002 - 2010.</p>
  433. <p>Distributed under the Boost Software License, Version 1.0. See
  434. <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
  435. <br>
  436. <br>
  437. </body>
  438. </html>