g++ 2.95.2 overload resolution does not follow ANSI standard

Dan Gehlhaar gehlhaar@Agouron.COM
Mon Jun 5 16:45:00 GMT 2000


Platform: IRIX 6.5
Compiler version: 2.95.2
Compiler flags: none

Description:

The following code compiles and runs as expected:

// --------------------------Start WORKS.C------------------------------

#include <iostream.h>

class A
{
public:
  template <class T, class U> void Printit (T t, U u)
    { cout << "Just template: " << t << " " << u << endl; }

  template <class T> void Printit (T t, double u)
    { cout << "Specialized template: " << t << " " << u << endl; }
};

main()
{
  A a;

  a.Printit ("Hello", 1);
  a.Printit ("There", 4.5);
}

// --------------------------End WORKS.C------------------------------


The following, however, exits with a compiler error stating that
it can't resolve the overload ambiguity:

// --------------------------Start FAILS.C------------------------------

#include <iostream.h>

class A
{
public:
  template <class T, class U> void Printit (T t, U u)
    { cout << "Just template: " << t << " " << u << endl; }

  template <class T> void Printit (T t, double u, int i = 0)
    { cout << "Specialized template: " << t << " " << u << endl; }
};

main()
{
  A a;

  a.Printit ("Hello", 1);
  a.Printit ("There", 4.5);
}

// --------------------------End FAILS.C------------------------------

The ANSI draft states that:

  --A candidate function having more than m parameters is viable only
    if the (m+1)-st parameter has a default argument.  For the
    purposes of overload resolution, the parameter list is truncated
    on the right, so that there are exactly m parameters.

( http://www.cygnus.com/misc/wp/dec96pub/over.html#over.match.viable )

So, for the purposes of overload resolution, these two programs should
behave identically.  If the first one works, so should the second one.
Or am I going loony?

Dan




More information about the Gcc-bugs mailing list