c++ - Templating a function to deduce the return type stl-container from input arguments -


i have function objective take stl-container of specific data type , return same stl-container of different specific data type. see below:

template < template <typename, typename = std::allocator<double> > class returncontainer, template <typename, typename = std::allocator<int> > class container     > inline returncontainer<double> transform(const container<int>& container) {   returncontainer<double> ret(container.size());    for(size_t = 0; < container.size(); ++i)   {     // here   }   return ret; } 

which use as:

// // using vectors // std::vector<int> data_vector_in; data_vector_in.push_back(0.0); data_vector_in.push_back(1.0);  std::vector<double> data_vector_out = transform<std::vector>(data_vector_in);  // // using lists // std::list<int> data_list_in; data_list_in.push_back(0.0); data_list_in.push_back(1.0);  std::list<double> data_list_out = transform<std::list>(data_list_in); 

how 1 (or can one) write return container can deduced input container?

std::vector<double> data_vector_out = transform(data_vector_in); // no <std::vector> 

although spirit of question similar the std::transform-like function returns transformed container, popular solution pointed template metaprogramming need avoid. solution using variadic template parameters solved issue.

i'm answering because seems hinting people doing in comments isn't helping out.

for vector-containers (e.g., vector , list, , not map) have single type associated them (e.g. vector<int>), can accept variadic template template parameter match container, must remember template definitions these containers contain things other type. example, allocator (which used, allows have control on memory allocation).

we forget allocator etc. because these defaulted.

here's new interface:

template <template<class...> class container, class... args> container<double> transform(const container<int, args...>& container); 

you explicit matching container of int , transforming container of double, replicated here.

the args... match allocator (and other template arguments after type). able ignore specifying allocator in return type because recall it, , other arguments after it, defaulted.

now can call so:

// using vectors std::vector<int> data_vector_in{0, 1}; auto data_vector_out = transform(data_vector_in);  // ensure got right type static_assert(std::is_same<decltype(data_vector_out), std::vector<double>>::value, "err");  // using lists std::list<int> data_list_in{0, 1}; auto data_list_out = transform(data_list_in); static_assert(std::is_same<decltype(data_list_out), std::list<double>>::value, "err"); 

live demo (c++14)


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -