0

With C++11 the Unified Initialization has been introduced and I got the impression that {} is now a quite safe initialization for all cases (when allowed). But I just noticed, that I mistaken.

What is a safe form of initialization for all types? Baring classes with deleted/private default c'tor, or course.

Details for some type-kinds

Here is a built-in:

int u;          // evil, x will be un-initialized
int v{};        // nice value-initialization, as far as I know.
int w={};       // nice value-initialization, right?
int x={0};      // proper initialization, not suited for other types though
int x=0;        // proper initialization, also not suited

But with an aggregate like tm

#include <ctime>
tm birth1{};    // evil. nothing initialized.
tm birth4=tm(); // yes! nice value-initialization.
tm birth2 ={};  // evil. nothing initialized
tm birth3 ={0}; // evil, only first member init

but with C-arrays

int data1[9] ={};  // evil. nothing initialized.
int data1[9] ={0}; // proper (value?)-initialization of all elements

And what with classes beyond aggregates with a compiler-generated default-c'tor?

struct Widget {
    std::string name;
    int val;
};
Widget w1;    // w1.val will be uninitialized
Widget w2{};  // w2.val will be uninitialized
Widget w3 = Widget{}; // ??? will w3.val be value-initialized?
Widget w4{0}; // an error, or course.

Did I forget an important kind?

A template that works for all?

As an extended question I wonder if there is a proper/good/any way to write a template function that returns a properly initialized object for all types that can be initialized in that way:

template<typename T>
T init() {
    return T{};
    // or 'T t = T{}; return t;'
    // or...
}
Community
  • 1
  • 1
towi
  • 21,587
  • 28
  • 106
  • 187
  • 3
    [dcl.init.aggr]/7 (from n3485) "If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from an empty initializer list" (i.e. value-initialized). – dyp Jan 23 '14 at 21:06
  • 2
    I think there's also been a defect, `Widget w2{};` will zero-initialize `w2` (and then call a non-trivial default-ctor). – dyp Jan 23 '14 at 21:08
  • [boost::value_init](http://www.boost.org/doc/libs/1_55_0/libs/utility/value_init.htm) – Mooing Duck Jan 23 '14 at 21:08
  • 2
    See [CWG 1301](http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#1301) and the related (linked) defects. Summing up my comments: As far as I know, after the defects have been fixed, `{}` will "properly" initialize an object/value. – dyp Jan 23 '14 at 21:09
  • 1
    Be aware that VS2013 does not implement the resolution to CWG 1301, continuing Visual C++'s long tradition of failing to correctly value-initialize. On an unrelated note, if you are paranoid about failing to initialize you should take a look at [AAA-style](http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/). – Casey Jan 23 '14 at 22:02
  • 1
    Maybe I should say that first I am interested in the *standard*s way to do it. Only second I am considering implementations -- those can be fixed. (Am I an romantic optimist?) – towi Jan 23 '14 at 22:13

0 Answers0