10

While writing a template, I want to initialize my variable to a value that serves as zero or null the the data type. If I set it to 0x00 is it going to serve as zero/NULL for any type ?

for example

This is template declaration

template <class T>
...
T A=0x00;

Now if I define an instance of type T => std::string the above statement serves as NULL ?

What about "int" and "unsigned int". For both of the it serves as "0" ?

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • `T`'s default constructor will take care of this for you. – erip Jan 05 '16 at 01:22
  • 1
    Unless `T` does not have a default constructor, that is. `T A;` will initialize `A` if `T` is a class type with a default constructor, but will not initialize `A` at all if `T` is an integer type, for instance. – Remy Lebeau Jan 05 '16 at 01:22
  • You're of course assuming that the type `T` has some kind of zero. This is generally only the case for numerical types. What would be the zero for `class Color` ? – MSalters Jan 05 '16 at 08:36

2 Answers2

22

Use Value Initialization:

T A = T(); // before C++11

T A{}; // C++11 and later

The effects of value initialization are:

1) if T is a class type with at least one user-provided constructor of any kind, the default constructor is called;
(until C++11)

1) if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized;
(since C++11)

2) if T is an non-union class type without any user-provided constructors, every non-static data member and base-class component of T is value-initialized;
(until C++11)

2) if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor;
(since C++11)

3) if T is an array type, each element of the array is value-initialized;

4) otherwise, the object is zero-initialized.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
8

You may use

T t{};

for value initialization.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    Note that this brace syntax for value initialization is C++11 only. – Remy Lebeau Jan 05 '16 at 01:28
  • @RemyLebeau: and your requires an accessible copy constructor :-) – Jarod42 Jan 05 '16 at 01:30
  • 2
    @RemyLebeau [Doesn't it?](http://coliru.stacked-crooked.com/a/25fbc8eec3622322) Even if the compiler performs copy elision, I still believe the copy ctor should be accessible. – vsoftco Jan 05 '16 at 01:34
  • @RemyLebeau Although item 1) of your answer seem to say otherwise... I tried compiling with `-std=c++98` also, and the code still doesn't compile (making the copy ctor `private` instead of `delete`d). I wonder whether cppreference is right here. – vsoftco Jan 05 '16 at 01:40
  • [Follow up question](http://stackoverflow.com/questions/34602933/do-we-need-an-accessible-copy-constructor-for-value-initialization-in-c98) – vsoftco Jan 05 '16 at 01:46