BE Student
BE Student

Reputation:

overload resolution in templates

Any one Explain this with an example

  "Involving conversions on a function argumentsinvolved in template
   parameter deduction."

EXamples like this:

 template<class T> struct B { /* ... */ };
 template<class T> struct D : public B<T> { /* ... */ };
 template<class T> void f(B<T>&);
 void g(B<int>& bi, D<int>& di)
   {
    f(bi);
    f(di);
   }

Please give me some more examples

EDIT: It is a point / statement from ISO C++ Standard 14.8.3/5th :Overload resolution

Upvotes: 1

Views: 380

Answers (1)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506837

It is about what the example shows. In sum, these are those conversions

  • The function parameter can be a Base<T>, while the function argument is a Derived<T>. Compare with ifstream << "hello" - left side of operator<< is deduced in that way.
  • The function parameter can be a const U&, while the function argument is a U (same for volatile).
  • The function parameter can be a const U* or const E C::* while the function argument is a U* or E C::* respectively (these are the qualification conversions) - same for volatile.

Other conversions cannot be done for a function parameter / argument that participate in deduction. The whole range of conversions can be applied if a function parameter does not participate in deduction, but this needs a non-deduced context or a context where there is no argument to be deduced at all, and implementation don't actually agree on it (see here).

In other words:

template<typename T> struct id { typedef T type; };
template<typename T> void f(T, typename id<T>::type);

int main() {
  // deduction acts on void(int*, int*) - deduction does not need
  // to deduce anything and follows [temp.arg.explicit]p4
  f<int*>(0, 0); 

  // deduction acts on void(T, id<T>::type) - second is a non-deduced context,
  // which *will* allow the conversion of int -> int*, since it will not compare
  // the argument with parameter during deduction (since it is non-deduced).
  f((int*)0, 0);
}

The second example is vital for some partial ordering contexts to work.

Upvotes: 3

Related Questions