This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFA] handling of 'operator' in cp_find_first_component


On Tue, 22 Apr 2003 10:33:05 -0400, Daniel Berlin <dberlin at dberlin dot org> said:

> To answer whether you need the return type, let's add two
> specializations here and make it worse:

> template <> long foo (int a)
> {
> 	return 9;
> }
> template <> int foo (int a)
> {
> 	return 10;
> }

Yeah, but that's illegal, isn't it?  You can't have two functions that
differ only in return type: otherwise, how would the compiler know
which one to use in a call to foo?

I tried it out in GCC; the above doesn't compile (I guess templates
with 0 parameters aren't legal), but when I compiled the following
file:

template <typename T> long foo(int a)
{
  return 9;
}
template <typename T> int foo (int a)
{
  return 10;
}

I got:

jackfruit$ g++ -c foo.cpp
foo.cpp:6: new declaration `template<class T> int foo(int)'
foo.cpp:2: ambiguates old declaration `template<class T> long int foo(int)'

Except that I don't understand C++ as well as I could: according to
<http://gcc.gnu.org/ml/gcc-patches/2000-02/msg00879.html>, there are
cases involving templated functions where the compiler is allowed to
disambiguate based on return type.  The above isn't one of them, and
even if I modify them to return objects of completely different types,
I still get a similar error message.

Hmm.  I'm confused.  When I play around with this further, life is
getting more bizarre: GCC happily compiles the following:

class C {};

template <typename T> int foo(int a)
{
  return C();
}
template <typename T> T foo (T a)
{
  return 10;
}

even though the first function is returning an object of the wrong
type!  Am I going crazy, or is that just a bug?  The first function
sure doesn't compile if I remove the template part.

At any rate, obviously I should try to read the standard or Stroustrup
to understand this better.

David Carlton
carlton at math dot stanford dot edu


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]