public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* ambiguous overload ?
@ 2000-07-05  2:19 Zhang Wei Feng
  2000-07-05  2:52 ` sidster
  0 siblings, 1 reply; 5+ messages in thread
From: Zhang Wei Feng @ 2000-07-05  2:19 UTC (permalink / raw)
  To: gcc

Hi,

I have the following question about overloading the following two functions.

int& Reg(string& name) ;
int Reg(string& name);

The g++ reports that:

CRegisterMap.c: In function `int & Reg(class basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > &)':
CRegisterMap.c:32: new declaration `int & Reg(class basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > &)'
CRegisterMap.c:24: ambiguates old declaration `int Reg(class basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > &)'

I thought g++ should make the difference between the two functions and will
call the functions correctly based on whether it is used as  lvalue or rvalue, that
is actually my purpose of overloading these two functions in this way!

Any suggestions,

Thanks for your attension!
weifeng

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ambiguous overload ?
  2000-07-05  2:19 ambiguous overload ? Zhang Wei Feng
@ 2000-07-05  2:52 ` sidster
  2000-07-05  3:27   ` Zhang Wei Feng
  0 siblings, 1 reply; 5+ messages in thread
From: sidster @ 2000-07-05  2:52 UTC (permalink / raw)
  To: Zhang Wei Feng; +Cc: gcc

* Zhang Wei Feng (weifeng.zhang@alcatel.be) [20000705 02:39]:
> 
> I have the following question about overloading the following two functions.
> 
> int& Reg(string& name) ;
> int Reg(string& name);


"Function that only differ in return type may not have the same name."
   ('The C++ Programming Language - 2nd Edition' B. Stroustrup p.586)

You can't overload function in C++ the way you want to.

patrick
--
Life is a waste of time, time is a waste of life, so get
wasted all of the time and have the time of your life.
   -- Michelle Mastrolacasa

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ambiguous overload ?
  2000-07-05  2:52 ` sidster
@ 2000-07-05  3:27   ` Zhang Wei Feng
  2000-07-05  6:54     ` Neil Booth
  0 siblings, 1 reply; 5+ messages in thread
From: Zhang Wei Feng @ 2000-07-05  3:27 UTC (permalink / raw)
  To: sidster; +Cc: gcc

Hi, patrick

Thanks for your inputs!

I have the following example working, though the difference is quite subtle!


#include <iostream>

using namespace std;

class Overload {
    int values[100];

public:
    // return coefficient reference for use as lvalue
    int& operator [](const int n) {
        return values[n];
    };

    // return coefficient for use as rvalue
    int operator [](const int n) const {
// NOTE without const, the compiler gets ambiguous error!
        return values[n];
    };

};


int main() {
    Overload over;

    for(int i = 0; i < 10 ; i++)
        over[i] = i;

    for(int i = 0; i < 10; i++)
        cout << "over[" << i << "] is " << over[i] << endl;

}

So as I noted above that if you forget the "const" for the rvalue operator,
then compiler still reports error. However, this do change the signature
of the function, but my question is

"Is that really difficult for normal functions( non-member functions) to differentiate
them by applying something like "const" " ?

Just for curiosity ;-)

weifeng




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ambiguous overload ?
  2000-07-05  3:27   ` Zhang Wei Feng
@ 2000-07-05  6:54     ` Neil Booth
  2000-07-05 13:48       ` Martin v. Loewis
  0 siblings, 1 reply; 5+ messages in thread
From: Neil Booth @ 2000-07-05  6:54 UTC (permalink / raw)
  To: Zhang Wei Feng; +Cc: sidster, gcc

Zhang Wei Feng wrote:-

> of the function, but my question is
> 
> "Is that really difficult for normal functions( non-member functions) to differentiate
> them by applying something like "const" " ?

I'm no expert, but I don't think the question is one of difficulty for
the compiler, but one of what is unambiguous in all cases (which can
get really hairy) and what promotes the coding style that Bjarne
wanted to promote at the time?

Neil.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ambiguous overload ?
  2000-07-05  6:54     ` Neil Booth
@ 2000-07-05 13:48       ` Martin v. Loewis
  0 siblings, 0 replies; 5+ messages in thread
From: Martin v. Loewis @ 2000-07-05 13:48 UTC (permalink / raw)
  To: NeilB; +Cc: weifeng.zhang, patrick, gcc

> I'm no expert, but I don't think the question is one of difficulty for
> the compiler, but one of what is unambiguous in all cases (which can
> get really hairy) and what promotes the coding style that Bjarne
> wanted to promote at the time?

In fact, the method case and the normal function case are the
same. For methods, there is an additional implicit argument whose
constness is declared in the end of the method signature; this
additional argument is also considered in overload resolution.

Regards,
Martin

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2000-07-05 13:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-07-05  2:19 ambiguous overload ? Zhang Wei Feng
2000-07-05  2:52 ` sidster
2000-07-05  3:27   ` Zhang Wei Feng
2000-07-05  6:54     ` Neil Booth
2000-07-05 13:48       ` Martin v. Loewis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).