C++ about generic initialization in templates -
C++ about generic initialization in templates -
i writing generic function below.
template<class iterator, class t> void foo(iterator first, iterator last) { t a; cout << << endl; // iterators } typedef vector<double>::iterator dblptr; vector<double> values; foo< dblptr, int>(); this functions prints out undefined value variable a, while if alter initialization into
/// t = t() cout << << endl; // iterators i can see initialized value 0 expecting.
if phone call t a variable initialized default value, if phone call t = t() believe due optimization re-create constructor should called value of t() still default one.
i cannot understand difference behind these 2 lines , reason why happens?
first of all, default initiaization of built-in types such int leaves them uninitialized. value initialization leaves them zero-initialized. example
this default initialization:
t a; this value initialization, using copy initialization:
t = t(); you right copies can elided here, has effect of creating single value-initialized t object. however, require t copyable or move-copyable. case built-in types, restriction bear in mind.
the re-create initialization syntax required because function declaration:
t a(); but c++11 allows value-initialize this:
t a{}; c++ templates initialization generic-programming function-templates
Comments
Post a Comment