C++: Default initialize an integral type in a template function -
for educational purposes wrting own c++ numerical vector template class. want able write (v, w) dot product of 2 vectors , consequently overload operator,() follows:
template<class t> const t vector<t>::operator,(const vector<t>& v) const { assertequalsize(v); t t; for(size_t i=0; i<numelements; i++) { t += elements[i] * v[i]; } return t; } my question is: how initialize t sensible value (e.g. 0.0 vector<double>)? tried t t(); g++ tells me, e.g., "double(*)()" cannot converted "const double" @ return statement , operator+=() not defined "(double(), double)".
thank much!
what need termed value initialization, has effect of zero-initializing built-in types:
t t{}; // c++11 t t = t(); // c++03 , c++11 the reason doesn't work
t t(); is declaration of parameterless function called t, returning t.
Comments
Post a Comment