c++ - Nested template parameters and type deduction -


hi practicing templates , type deduction , wanted try making simple function template nested template parameters print out contents of stl container:

template <template<t, alloc> cont>  void print(const cont<t, alloc> &c) {      (const t &elem : c) std::cout << elem << " ";     std::cout << std::endl; } 

and test case:

int main() {     std::list<int> intlist{ 1, 2, 3, 4, 5 };     std::vector<float> floatvec{ 0.2f, 0.5f };      print(intlist);     print(floatvec); } 

however getting compiler error whereby types t , alloc cannot deduced. there way me write function without having explicit state types template arguments?

note object here able deduce type stored within passed in stl container. hence if vector of ints passed in t deduced type int.

in case, may do

template <typename cont>  void print(const cont& c) {     (const auto& elem : c) std::cout << elem << " ";     std::cout << std::endl; } 

if want restrict print function template classes 2 template arguments, syntax be:

template <template<typename , typename> class cont, typename t, typename alloc>  void print(const cont<t, alloc> &c) {     (const t &elem : c) std::cout << elem << " ";     std::cout << std::endl; } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -