c++ - Template Alias failing when I try to use it in a function -



c++ - Template Alias failing when I try to use it in a function -

i trying utilize template type aliasing, when utilize in function thought fails, in illustration below:

#include <iostream> #include <vector> template <typename t> using vec_t = std::vector<t>; template <typename t> t sum_vector(vec_t vec) // t sum_vector(std::vector<t> vec) { t sum = 0; typename vec_t::iterator it; // typename std::vector<t>::iterator it; (it = vec.begin(); != vec.end(); ++it) { sum += *it; } homecoming sum; }

compiling above code fails next error:

error: template declaration of ‘t sum_vector’ t sum_vector(vec_t vec)

error: missing template arguments before ‘vec’ t sum_vector(vec_t vec)

if used commented lines instead code works fine. don't understand missing here, understanding after using x = y, compiler place y have x, error coming from? how should go fixing this?

the difference between using x = y , using vec_t = std::vector<t>; former type alias , latter alias template (or template alias, whatever.) first form typedef. sec form doesn't automatically substitute t. need specialize it. set between angle brackets <> substituted t in alias template. vec_t<int> becomes std::vector<int>.

§ 14.6/2

when template-id refers specialization of alias template, equivalent associated type obtained substitution of template-arguments template-parameters in type-id of alias template.

template<class t> struct alloc { /∗ ... ∗/ }; template<class t> using vec = vector<t, alloc<t>>; vec<int> v; // same vector<int, alloc<int>> v;

c++ templates c++11

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -