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?