www.digitalmars.com         C & C++   DMDScript  

c++ - partial ordering of template functions

reply Christof Meerwald <cmeerw web.de> writes:
#include <stdio.h>

template<class T>
struct A
{ };

struct B
{ };

template<class U, class T>
A<U> f(T t)
{
  printf("1\n");
  return A<U>();
}

template<class T>
A<B> f(T t)
// Error: 'f' previously declared as something else
// It was declared as: A<U>C func(T)
// It is now declared: A<B>C func(T)
{
  printf("2\n");
  return A<B>();
}

int main()
{
  f(0);
  // Error: ambiguous reference to symbol
  f<int>(0);
  // Error: ambiguous reference to symbol

  return 0;
}


Extracted from Boost.Bind (to get adaptable function objects working).

gcc-3.0 chooses the second function for "f(0)" and the first one for
"f<int>(0)" and I guess that's what Boost.Bind expects.


bye, Christof

-- 
http://cmeerw.org                                 JID: cmeerw jabber.at
mailto cmeerw at web.de

...and what have you contributed to the Net?
Dec 23 2002
parent reply "Walter" <walter digitalmars.com> writes:
I've fixed the first issue, 'Error: 'f' previously declared as something
else'. But I think the other two should be ambiguous, I cannot find a rule
that gives the results you are seeing from gcc-3.0.


"Christof Meerwald" <cmeerw web.de> wrote in message
news:au7ab9$67p$1 digitaldaemon.com...
 #include <stdio.h>

 template<class T>
 struct A
 { };

 struct B
 { };

 template<class U, class T>
 A<U> f(T t)
 {
   printf("1\n");
   return A<U>();
 }

 template<class T>
 A<B> f(T t)
 // Error: 'f' previously declared as something else
 // It was declared as: A<U>C func(T)
 // It is now declared: A<B>C func(T)
 {
   printf("2\n");
   return A<B>();
 }

 int main()
 {
   f(0);
   // Error: ambiguous reference to symbol
   f<int>(0);
   // Error: ambiguous reference to symbol

   return 0;
 }


 Extracted from Boost.Bind (to get adaptable function objects working).

 gcc-3.0 chooses the second function for "f(0)" and the first one for
 "f<int>(0)" and I guess that's what Boost.Bind expects.


 bye, Christof

 --
 http://cmeerw.org                                 JID: cmeerw jabber.at
 mailto cmeerw at web.de

 ...and what have you contributed to the Net?
Dec 31 2002
parent reply Christof Meerwald <cmeerw web.de> writes:
On Tue, 31 Dec 2002 14:18:54 -0800, Walter wrote:
 I've fixed the first issue, 'Error: 'f' previously declared as something
 else'. But I think the other two should be ambiguous, I cannot find a rule
 that gives the results you are seeing from gcc-3.0.
 "Christof Meerwald" <cmeerw web.de> wrote in message
 news:au7ab9$67p$1 digitaldaemon.com...
 #include <stdio.h>

 template<class T>
 struct A
 { };

 struct B
 { };

 template<class U, class T>
 A<U> f(T t)
 {
   printf("1\n");
   return A<U>();
 }

 template<class T>
 A<B> f(T t)
 {
   printf("2\n");
   return A<B>();
 }

 int main()
 {
   f(0);
   // Error: ambiguous reference to symbol
See 14.8.3 Overload resolution [temp.over] "If, for a given function template, argument deduction fails, no such function is added to the set of candidate functions for that template." And argument deduction should fail for "template<class U, class T> A<U> f(T t)" (because U can't be deduced), therefore it should not be added to the set of candidate functions leaving "template<class T> A<B> f(T t)" as the only possible candidate.
   f<int>(0);
   // Error: ambiguous reference to symbol
See 14.5.5.2 Partial ordering of function templates [temp.func.order], paragraphes 3 and 4. "template<class U, class T> A<U> f(T t)" is at least as specialized as "template<class T> A<B> f(T t)". But, "template<class T> A<B> f(T t)" is not at least as specialized as "template<class U, class T> A<U> f(T t)" (argument deduction fails because U can't be deduced from the function parameter list) Thus "template<class U, class T> A<U> f(T t)" is more specialized than "template<class T> A<B> f(T t)" and should therefore be selected. That's my interpretation of the standard... bye, Christof PS: Happy New Year! :-) -- http://cmeerw.org JID: cmeerw jabber.at mailto cmeerw at web.de ...and what have you contributed to the Net?
Dec 31 2002
parent "Walter" <walter digitalmars.com> writes:
All right. That does make some sense. I'll check it out. -Walter

"Christof Meerwald" <cmeerw web.de> wrote in message
news:autg6u$1e2h$1 digitaldaemon.com...
 On Tue, 31 Dec 2002 14:18:54 -0800, Walter wrote:
 I've fixed the first issue, 'Error: 'f' previously declared as something
 else'. But I think the other two should be ambiguous, I cannot find a
rule
 that gives the results you are seeing from gcc-3.0.
 "Christof Meerwald" <cmeerw web.de> wrote in message
 news:au7ab9$67p$1 digitaldaemon.com...
 #include <stdio.h>

 template<class T>
 struct A
 { };

 struct B
 { };

 template<class U, class T>
 A<U> f(T t)
 {
   printf("1\n");
   return A<U>();
 }

 template<class T>
 A<B> f(T t)
 {
   printf("2\n");
   return A<B>();
 }

 int main()
 {
   f(0);
   // Error: ambiguous reference to symbol
See 14.8.3 Overload resolution [temp.over] "If, for a given function template, argument deduction fails, no such function is added to the set of candidate functions for that template." And argument deduction should fail for "template<class U, class T> A<U>
f(T
 t)" (because U can't be deduced), therefore it should not be added to the
 set of candidate functions leaving "template<class T> A<B> f(T t)" as the
 only possible candidate.


   f<int>(0);
   // Error: ambiguous reference to symbol
See 14.5.5.2 Partial ordering of function templates [temp.func.order], paragraphes 3 and 4. "template<class U, class T> A<U> f(T t)" is at least as specialized as "template<class T> A<B> f(T t)". But, "template<class T> A<B> f(T t)" is not at least as specialized as "template<class U, class T> A<U> f(T t)" (argument deduction fails because
U
 can't be deduced from the function parameter list)

 Thus "template<class U, class T> A<U> f(T t)" is more specialized than
 "template<class T> A<B> f(T t)" and should therefore be selected.


 That's my interpretation of the standard...


 bye, Christof

 PS: Happy New Year! :-)

 --
 http://cmeerw.org                                 JID: cmeerw jabber.at
 mailto cmeerw at web.de

 ...and what have you contributed to the Net?
Dec 31 2002