www.digitalmars.com         C & C++   DMDScript  

c++ - (template) overload resolution

reply Christof Meerwald <cmeerw web.de> writes:
Here is a small test-case:

#include <stdio.h>

template<class T>
int f(T *)
{
  return 0;
}

template<class T>
int f(const T *)
{
  return 1;
}


int g(char *)
{
  return 0;
}

int g(const char *)
{
  return 1;
}


template<class T>
int h(T *, T *)
{
  return 0;
}

template<class T>
int h(const T *, T *)
{
  return 1;
}


int i(char *, char *)
{
  return 0;
}

int i(const char *, char *)
{
  return 1;
}


int main(int argc, char *argv[])
{
  char a = 0;

  printf("%d\n", f(&a));
  printf("%d\n", g(&a));
  printf("%d\n", h(&a, &a));
  printf("%d\n", i(&a, &a));

  return 0;
}


I would expect to get four times "0" (and that's what I get with gcc
3.0.1/2.95.3 and Watcom 11.0c), but DM chooses "int f(const T *)" instead of
"int f(T *)" and doesn't like the h function template at all:
  Error: ambiguous reference to symbol
  Had: h(T*,T*)
  and: h(const T*,T*)


bye, Christof

-- 
http://cmeerw.cjb.net                          Jabber: cmeerw jabber.at
mailto cmeerw at web.de                   ICQ: 93773535, Yahoo!: cmeerw

...and what have you contributed to the Net?
Oct 22 2001
parent reply Christof Meerwald <cmeerw web.de> writes:
Here is a somewhat related test-case:

struct A
{ };

struct B
  : public A
{ };


template<class T>
int f(T a, const A&)
{
  return 1;
}

template<class T>
int f(T a, const B&)
{
  return 0;
}


int main(int argc, char *argv[])
{
  char c = 0;

  return f(c, B());
  // Error: ambiguous reference to symbol
  // Had: f(T,const A&)
  // and: f(T,const B&)
}


bye, Christof

-- 
http://cmeerw.cjb.net                          Jabber: cmeerw jabber.at
mailto cmeerw at web.de

...and what have you contributed to the Net?
Oct 31 2001
next sibling parent "Walter" <walter digitalmars.com> writes:
Got it!

"Christof Meerwald" <cmeerw web.de> wrote in message
news:9roldc$1n0a$1 digitaldaemon.com...
 Here is a somewhat related test-case:

 struct A
 { };

 struct B
   : public A
 { };


 template<class T>
 int f(T a, const A&)
 {
   return 1;
 }

 template<class T>
 int f(T a, const B&)
 {
   return 0;
 }


 int main(int argc, char *argv[])
 {
   char c = 0;

   return f(c, B());
   // Error: ambiguous reference to symbol
   // Had: f(T,const A&)
   // and: f(T,const B&)
 }


 bye, Christof

 --
 http://cmeerw.cjb.net                          Jabber: cmeerw jabber.at
 mailto cmeerw at web.de

 ...and what have you contributed to the Net?
Oct 31 2001
prev sibling parent Christof Meerwald <cmeerw web.de> writes:
Any chance this will be fixed any time soon? (it doesn't work with DMC
8.29.6n beta)

I am asking because STLport 4.5.3 uses these constructs quite often.

On Wed, 31 Oct 2001 10:56:44 +0000 (UTC), Christof Meerwald wrote:
 struct A
 { };
 
 struct B
  : public A
 { };
 
 
 template<class T>
 int f(T a, const A&)
 {
   return 1;
 }
 
 template<class T>
 int f(T a, const B&)
 {
   return 0;
 }
 
 
 int main(int argc, char *argv[])
 {
   char c = 0;
 
   return f(c, B());
   // Error: ambiguous reference to symbol
   // Had: f(T,const A&)
   // and: f(T,const B&)
 }
bye, Christof -- http://cmeerw.org JID: cmeerw jabber.at mailto cmeerw at web.de ...and what have you contributed to the Net?
Jul 07 2002