I am writing a generic function like below.
template<class Iterator, class T>
void foo(Iterator first, Iterator last) {
T a;
cout << a << endl;
// do something with iterators
}
typedef vector<double>::iterator DblPtr;
vector<double> values;
foo< DblPtr, int>();
This functions prints out an undefined value for variable a
, while if I change the initialization into
///
T a = T()
cout << a << endl;
// do something with iterators
I can see that the initialized value is 0
as I am expecting.
If I call T a
the variable is initialized with the default value, but if i call T a = T()
I believe that due to optimization the copy constructor should be called with the value of T()
that is still the default one.
I cannot understand what is the difference behind these 2 lines and the reason why this happens?