[RFA] handling of 'operator' in cp_find_first_component
David Carlton
carlton@math.stanford.edu
Wed Apr 23 23:46:00 GMT 2003
On Tue, 22 Apr 2003 10:33:05 -0400, Daniel Berlin <dberlin@dberlin.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@math.stanford.edu
More information about the Gdb-patches
mailing list