1

Consider the following code:

template<class T>
void someFun() {
    T t;
    // stuff with t
}

Here I can be sure that if T is a default-constructible struct or class then t should be initialized to its default value.

However, if T is, for example, an int, then it will be uninitialized here. Well, the intuitive "default initialization" for an int should mean that it should be initialized to zero.

What I'm looking for is a way to call the default constructor for T if T is of a class type and to initialize it to zero if it is a scalar type.

Will this work?

template<class T>
void someFun() {
    T t{};
    // stuff with t
}

AFAIK in the above example the default contructor for T will be called if its a class type, but can I be sure that it will be zero if it is an int or another scalar type?

  • I'm 99% sure the answer is yes. At the very least, I know that `int foo = {}` initializes it to 0. Searching for a source... – Justin Oct 30 '17 at 18:34
  • "Will this work?" Well, did you try it? Did it work? – DimChtz Oct 30 '17 at 18:34
  • 2
    @DimChtz That doesn't work so well with C++. It may well appear to work, but not actually work – Justin Oct 30 '17 at 18:35
  • AFAIK, `T t{};` guarantees that the value will be default-constructed and initialized, so long as a default-construction exists for that type, which it does for all fundamental types. – Xirema Oct 30 '17 at 18:35
  • @DimChtz https://ideone.com/weZQN4 Yes I did try it HOWEVER this is C++ for sake, it is perfectly plausible to assume that unless I have a guarantee it will work 99% of times and make `t` contain some garbage in this 1% of times. –  Oct 30 '17 at 18:36
  • 1
    [Yes it's guaranteed](http://en.cppreference.com/w/cpp/language/zero_initialization) – Justin Oct 30 '17 at 18:36
  • Related, maybe possible duplicate of? https://stackoverflow.com/q/11164394/1896169 – Justin Oct 30 '17 at 18:38
  • @Justin Make an answer from this cppreference link, so I can accept it :) –  Oct 30 '17 at 18:39
  • Also related: https://stackoverflow.com/q/14259602/1896169 – Justin Oct 30 '17 at 18:40

2 Answers2

3

For scalar types {} intializer triggers so-called value-initialization. And, for scalar types value-initialization leads to zero-initialization. So, yes, it will zero-initialize objects of scalar types.

For class types with default constructor it also triggers value-initialization, which leads to default-initialization (class with user-provided default constructor) or zero-initialization (class without user-provided or deleted default constructor).

So, it appears to do what you want it do do in all cases you mentioned.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • I think the question wants something that works for non-scalar types too, which this still does in most cases. – Daniel H Oct 30 '17 at 21:38
  • Aggregate initialization differs as usual: for class types which are aggregates there is no zero-initialization of the whole object and no constructor invocation; instead, each non-static data member without a brace-or-equal-initializer is initialized as if by `{}` – M.M Oct 30 '17 at 23:16
0

Justin already dug up the correct reference in the comments (it's called zero-initialization), so yes, this works for all class and non-class types. Not just integral types, but also bools, floating-point types and pointers.

It doesn't work for someFun<void>() though, but I'd say that function is devoid of fun.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • The general term is “[value-initialized](http://en.cppreference.com/w/cpp/language/value_initialization)”, which does zero-initialization for fundamental types. This syntax might do [aggregate-initialization](http://en.cppreference.com/w/cpp/language/aggregate_initialization) or [list-initialization](http://en.cppreference.com/w/cpp/language/list_initialization) for some types instead, In any case, it should probably do what you want. – Daniel H Oct 30 '17 at 21:36