rule for template deduction for overloaded recursive function in c++ -


i have found lot of information template deduction (eg c++ templated function overloading rules) not me understand behaviour of template deduction overloaded recursive function. in following code, not understand how compiler managed deduce should use vector<t> function 2 times vectvect , pair<t,u> 2 times pairpair - can. thus, not understand why can not deduce should use both vector<t> , pair<t,u> function vectpair ?

is why const keyword leads increase conversion , make t function better ? (but how 2 other examples can work in case ?)

is 2 first deduction possible because current function tested first template deduction in recursive call ?

#include <iostream> #include <sstream> #include <vector>  using namespace std;  template<class t> string print(const t& t){     ostringstream s;     s << t;     return s.str(); }  template<class t> string print(const vector<t>& t){     ostringstream s;     s << '[';     for(int i=0;i<(int)t.size();i++)         s << print(t[i]) << ' ';     s << ']';     return s.str(); }  template<class t,class u> string print(const pair<t,u>& t){     ostringstream s;     s << '(' << print(t.first) << ',' << print(t.second) << ')';     return s.str(); }  int main ( int argc, char **argv ) {     vector<vector<double> > vectvect(4,vector<double>(4));     for(int i=0;i<(int)4;i++)         for(int j=0;j<(int)4;j++)             vectvect[i][j] = i*4+j;     pair<int,pair<string,double> > pairpair = make_pair(10, make_pair("foo",0.5));     vector<pair<int,string> > vectpair(1,make_pair(42,"bar"));      ///template deduction     cout << print(vectvect) << endl;     cout << print(pairpair) << endl;      ///template deduction failure     //====> here problem      //cout << print(vectpair) << endl;      return 0; } 

currently, trying understand, if knows how without introducing large source overhead, interested.

thank you.

the problem not related template argument deduction, neither overload resolution. print overload taking pair not picked compiler because can't found unqualified name lookup, neither adl. should either reorder 2 definitions of functions, 1 taking pair comes first:

template <class t,class u> string print(const pair<t,u>& t){     /**/ }  template <class t> string print(const vector<t>& t){     /**/ } 

or declare functions before define , use them:

template <class t> string print(const t& t); template <class t,class u> string print(const pair<t,u>& t);     template <class t> string print(const vector<t>& t); 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -