Given the following function template from a tutorial.
template<class T>
double GetAverage(T tArray[], int nElements)
{
T tSum = T(); // tSum = 0
for (int nIndex = 0; nIndex < nElements; ++nIndex)
{
tSum += tArray[nIndex];
}
// Whatever type of T is, convert to double
return double(tSum) / nElements;
}
In the line
T tSum = T(); // tSum = 0
they are saying it will call the default constructor for particular type (based on the type we call this function). My doubt is how this call assigning the value to tSum, as this will call the constructor fine. but since constructor does not return anything how iSum is initialized to 0 for int or 0.0 for double.
The tutorial you are reading makes some serious terminological distortions/simplifications. The statement saying that
T tSum = T();
calls "default constructor" is incorrect. It is immediately obvious from the fact that in general case type T
can easily be a non-class type. Non-class types don't have any constructors, yet the above initialization is valid for them as well.
The proper term in this case is value-initialization. Expression T()
produces a temporary object of type T
initialized by value-initialization process. Value-initialization works in accordance with its own specific rules, and it does not necessarily involve any constructors at all. It proceeds in completely constructor-less ways for non-class types as well as for some categories of class types.
For example, expression int()
produces value 0
of type int
- that is what value-initialization means for type int
(and for all scalar types). It, of course, does not involve any "default constructors", since type int
can't possibly have any constructors.
Again, expression T()
in not a constructor call, as that tutorial seems to incorrectly state. Expression T()
is actually a functional-style cast with no operand. A functional-style cast with no operand produces, as I said above, a value-initialized temporary object of type T
. It does not depend on the constructor "returning" anything.
The temporary value if T()
expression is then used as initializer for tSum
object. This syntax invokes copy initialization of tSum
from T()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With