c++ - Template argument deduction from function body -


if we've function template,

template<typename t> void f(t param) {} 

then can call in following ways,

int i=0; f<int>(i);//t=int : no need deduce t f(i); //t=int : deduced t function argument!  //likewise sample s; f(s); //t=sample : deduced t function argument! 

now consider variant of above function template,

template<typename targ, typename tbody> void g(targ param)  {    tbody v=param.member; } 

now, can compiler deduce template arguments if write,

sample s; g(s); //targ=sample, tbody=int?? 

suppose sample defined as,

struct sample {    int member; }; 

there 2 questions:

  • can compiler deduce template arguments in second example?
  • if no, why? there difficulty? if standard doesn't "template argument deduction function body", because argument(s) cannot deduced? or didn't consider such deduction avoid adding more complexity language? or what?

i know views on such deduction.


edit:

by way, gcc able deduce function arguments if write code:

template<typename t> void h(t p) {         cout << "g() " << p << endl;         return; } template<typename t> void g(t p) {         h(p.member); //if here gcc can deduce t h(), why not tbody in previous example?         return; } 

working demonstration example : http://www.ideone.com/cvxea

not working demonstration previous example: http://www.ideone.com/ux038

you concluded compiler won't deduce tbody examining type of sample.member. add yet level of complexity template deduction algorithm.

the template matching algorithm considers function signatures, not bodies. while not used often, it's legal declare templated function without providing body:

template <typename t> void f(t param); 

this satisfies compiler. in order satisfy linker, must of course define function body somewhere, , ensure required instantiations have been provided. function body not have visible client code of template function, long required instantiations available @ link time. body have explicitly instantiate function, eg:

template <> void f(int param); 

but partially applies questions, because imagine scenario following, 2nd parameter deduced provided default parameter, , won't compile:

template<typename targ, typename tbody> void g(targ param, tbody body = param.member);  // won't deduce tbody targ 

the template matching algorithm considers actual type, not potential nested member types in case of classes or structs. have added level of complexity apparently judged complex. should algorithm stop? members of members, , forth, considered?

also, it's not required because there other means of achieving same intention, shown in example below.

nothing prevents writing:

struct sample {    typedef int membertype;    membertype member; };  template<typename targ> void g(targ param)  {    typename targ::membertype v = param.member; }  sample s = { 0 }; g(s); 

in order obtain same effect.


regarding sample added after editing: whereas seems h(p.member) depend on member of struct, , hence template matching algorithm should fail, doesn't because made two-step process:

  1. upon seeing g(s);, compiler looks function taking argument of type sample (templated or not!). in case, best match void g(t p). at point, compiler has not looked @ body of g(t p) yet!.
  2. now, compiler creates instance of g(t p), specialized t: sample. when sees h(p.member) knows p.member of type int, , try locate function h() taking argument of type int. template function h(t p) turns out best match.

note if had written (note not_a_member):

template<typename t> void g(t p) {         h(p.not_a_member);         return; } 

then compiler still consider g() valid match @ stage 1. error when turns out sample not have member called not_a_member.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -