473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Why is overloading operator. (member operator) forbidden?

Hello,

I was wondering, why is overloading operator. (period) forbidden? It
would make a few odd applications possible (dynamic inheritance and
transparent remote method invocation spring to my mind) and it would
be fairly generic. The only sidecase I can see is that operator.
itself would not be looked up through operator. .

I read that there was previous debate on the subject, but I haven't
been able to find why it was rejected.

I sent this message to the Boost mailing list, in error, they referred
me to Usenet.

Thanks,

Peter

May 14 '07 #1
11 4110
On 14 Maj, 07:36, "dasca...@gmail.com" <dasca...@gmail.comwrote:
Hello,

I was wondering, why is overloading operator. (period) forbidden? It
would make a few odd applications possible (dynamic inheritance and
transparent remote method invocation spring to my mind) and it would
be fairly generic. The only sidecase I can see is that operator.
itself would not be looked up through operator. .

I read that there was previous debate on the subject, but I haven't
been able to find why it was rejected.

I sent this message to the Boost mailing list, in error, they referred
me to Usenet.
There are two reasons against it that I know of, the first being that
the general consensus in the standard committee is that it might
introduce too much trouble* , since a user would never know what will
happen if he uses the .-operator, the second is the problem you
mentioned about how to implement the normal behaviour. I know of no
convincing reason for allowing it since it really would not enable
behaviour that can't be simulated in other ways.

* In fact a recent proposal (n2200) suggests that all operators,
except . (member selection), :: (namespace) and .* (memberpointer),
should be overloadable both as members and non member (that includes
new and delete).

--
Erik Wikström

May 14 '07 #2
On 14 mei, 08:12, Erik Wikström <eri...@student.chalmers.sewrote:
There are two reasons against it that I know of, the first being that
the general consensus in the standard committee is that it might
introduce too much trouble* , since a user would never know what will
happen if he uses the .-operator, the second is the problem you
mentioned about how to implement the normal behaviour. I know of no
convincing reason for allowing it since it really would not enable
behaviour that can't be simulated in other ways.
I was thinking of a few things in particular, mainly allowing
transparent forwarding of interfaces. Given variadic templates and an
overloadable operator. you could define a function as such:

template <typename R, typename... Argtypes>
auto operator.(std::string funcname, Argtypes... args) { return
memfun(this, new gen_send<R, ArgTypes...>(funcname); }
template <typename R, typename... Argtypes>
class gen_send {
private:
string funcname;
public:
R &operator()(Argtypes... arguments) {
// serialize and send
// wait for return value, return value
}
};

that would allow transparently forwarding of the function called,
irrelevant of which function it was. Marshalling the return type might
cause one or two small bits of trouble but I think there's a fairly
easy way around that.
* In fact a recent proposal (n2200) suggests that all operators,
except . (member selection), :: (namespace) and .* (memberpointer),
should be overloadable both as members and non member (that includes
new and delete).
Wouldn't that cause trouble with typeid and especially sizeof?

May 14 '07 #3
On May 14, 8:12 am, Erik Wikström <eri...@student.chalmers.sewrote:
On 14 Maj, 07:36, "dasca...@gmail.com" <dasca...@gmail.comwrote:
I was wondering, why is overloading operator. (period) forbidden? It
would make a few odd applications possible (dynamic inheritance and
transparent remote method invocation spring to my mind) and it would
be fairly generic. The only sidecase I can see is that operator.
itself would not be looked up through operator. .
I read that there was previous debate on the subject, but I haven't
been able to find why it was rejected.
There are two reasons against it that I know of, the first being that
the general consensus in the standard committee is that it might
introduce too much trouble* , since a user would never know what will
happen if he uses the .-operator,
By that standard, you couldn't overload any of the operators.
the second is the problem you
mentioned about how to implement the normal behaviour.
The answer to that is also well known: (&obj)->.
I know of no
convincing reason for allowing it since it really would not enable
behaviour that can't be simulated in other ways.
Smart references and proxies.
* In fact a recent proposal (n2200) suggests that all operators,
except . (member selection), :: (namespace) and .* (memberpointer),
should be overloadable both as members and non member (that includes
new and delete).
Hmmm. I'll have to look at it, but I'd be very surprised if
they allow the assignment operator to be a non-member. That
would cause no end of problems. (I'm also curious with regards
to ?:. I don't think I'd like to see it overloadable unless
there was a requirement that the first argument be a user
defined type.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 14 '07 #4
On May 14, 8:12 am, Erik Wikström <eri...@student.chalmers.sewrote:
* In fact a recent proposal (n2200) suggests that all operators,
except . (member selection), :: (namespace) and .* (memberpointer),
should be overloadable both as members and non member (that includes
new and delete).
Just a note, but the proposal *does* propose allowing . and .*
to be overloaded. It also retains the restriction that copy
assignment must be a member.

Regretfully, it doesn't seem to say much about the syntax of the
new overloads; just saying that "." should be added to the table
of overloadable operators isn't enough, since it does need some
special treatment. (Like ->, it is a binary operator, but the
second operand is something you can't pass as a parameter.)

Note that anybody can propose anything, and the committee is
required to enregister the proposal, and vote on it. In this
case, the proposal does seem serious, but I fear that it is too
late for it to receive serious consideration. The proposal to
allow overloading operator new and operator delete as
non-members, in particular, would require significant
discussion---to begin with, I'm not sure it's implementable, and
their proposal certainly seems ambiguous with current use (and
would break significant amounts of existing code). So I fear
that the proposal is dead on arrival.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 14 '07 #5
On 14 Maj, 09:33, "dasca...@gmail.com" <dasca...@gmail.comwrote:
On 14 mei, 08:12, Erik Wikström <eri...@student.chalmers.sewrote:
There are two reasons against it that I know of, the first being that
the general consensus in the standard committee is that it might
introduce too much trouble* , since a user would never know what will
happen if he uses the .-operator, the second is the problem you
mentioned about how to implement the normal behaviour. I know of no
convincing reason for allowing it since it really would not enable
behaviour that can't be simulated in other ways.

I was thinking of a few things in particular, mainly allowing
transparent forwarding of interfaces. Given variadic templates and an
overloadable operator. you could define a function as such:

template <typename R, typename... Argtypes>
auto operator.(std::string funcname, Argtypes... args) { return
memfun(this, new gen_send<R, ArgTypes...>(funcname); }
template <typename R, typename... Argtypes>
class gen_send {
private:
string funcname;
public:
R &operator()(Argtypes... arguments) {
// serialize and send
// wait for return value, return value
}

};

that would allow transparently forwarding of the function called,
irrelevant of which function it was. Marshalling the return type might
cause one or two small bits of trouble but I think there's a fairly
easy way around that.
* In fact a recent proposal (n2200) suggests that all operators,
except . (member selection), :: (namespace) and .* (memberpointer),
should be overloadable both as members and non member (that includes
new and delete).

Wouldn't that cause trouble with typeid and especially sizeof?
Ah, I missed those. I didn't read the whole of the proposal, but I
don't think those were included (nor ?:).

--
Erik Wikström

May 14 '07 #6
On 14 Maj, 10:35, James Kanze <james.ka...@gmail.comwrote:
On May 14, 8:12 am, Erik Wikström <eri...@student.chalmers.sewrote:
On 14 Maj, 07:36, "dasca...@gmail.com" <dasca...@gmail.comwrote:
I was wondering, why is overloading operator. (period) forbidden? It
would make a few odd applications possible (dynamic inheritance and
transparent remote method invocation spring to my mind) and it would
be fairly generic. The only sidecase I can see is that operator.
itself would not be looked up through operator. .
I read that there was previous debate on the subject, but I haven't
been able to find why it was rejected.
There are two reasons against it that I know of, the first being that
the general consensus in the standard committee is that it might
introduce too much trouble* , since a user would never know what will
happen if he uses the .-operator,

By that standard, you couldn't overload any of the operators.
the second is the problem you
mentioned about how to implement the normal behaviour.

The answer to that is also well known: (&obj)->.
And if operator-is overloaded?
I know of no
convincing reason for allowing it since it really would not enable
behaviour that can't be simulated in other ways.

Smart references and proxies.
* In fact a recent proposal (n2200) suggests that all operators,
except . (member selection), :: (namespace) and .* (memberpointer),
should be overloadable both as members and non member (that includes
new and delete).

Hmmm. I'll have to look at it, but I'd be very surprised if
they allow the assignment operator to be a non-member. That
would cause no end of problems. (I'm also curious with regards
to ?:. I don't think I'd like to see it overloadable unless
there was a requirement that the first argument be a user
defined type.)
operator= was one of them specifically mentioned, (along with *_cast)
but I think that I was too quick when I said all operators, I don't
thin ?: was included.

--
Erik Wikström

May 14 '07 #7

"Erik Wikström" <er****@student.chalmers.sewrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
On 14 Maj, 10:35, James Kanze <james.ka...@gmail.comwrote:
On May 14, 8:12 am, Erik Wikström <eri...@student.chalmers.sewrote:
the second is the problem you
mentioned about how to implement the normal behaviour.
The answer to that is also well known: (&obj)->.

And if operator-is overloaded?
You can't overload operators for pointers, so that shouldn't matter.
However, you can overload the unary & to not return a pointer to your object
(for example, in case of smart references, a typical & operator would return
a smart pointer). Btw, there is a proposal to overload the . and .*
operators:
http://www.open-std.org/jtc1/sc22/wg...2004/n1671.pdf. One of
the proposed (and imho the best) ways to get the old behaviour if . were to
be overloaded, is:

addressof(x)->x_member

where addressof(x) is the function as declared in boost:

template<class TT * addressof(T & t)
{
return reinterpret_cast<T*>(
&const_cast<char*>(
reinterpret_cast<const volatile char &>(t)));
}

- Sylvester
May 14 '07 #8
"James Kanze" <ja*********@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
On May 14, 8:12 am, Erik Wikström <eri...@student.chalmers.sewrote:
* In fact a recent proposal (n2200) suggests that all operators,
except . (member selection), :: (namespace) and .* (memberpointer),
should be overloadable both as members and non member (that includes
new and delete).

Just a note, but the proposal *does* propose allowing . and .*
to be overloaded. It also retains the restriction that copy
assignment must be a member.

Regretfully, it doesn't seem to say much about the syntax of the
new overloads; just saying that "." should be added to the table
of overloadable operators isn't enough, since it does need some
special treatment. (Like ->, it is a binary operator, but the
second operand is something you can't pass as a parameter.)
It's in a seperate (earlier) proposal:
http://www.open-std.org/jtc1/sc22/wg...2004/n1671.pdf. The
recently published new state of C++ evolution
(http://www.open-std.org/jtc1/sc22/wg...007/n2228.html) now
also states it as an active topic in Evolution, which surely sounds
promising.

- Sylvester
May 14 '07 #9
On May 14, 10:52 am, Erik Wikström <eri...@student.chalmers.sewrote:
On 14 Maj, 10:35, James Kanze <james.ka...@gmail.comwrote:
On May 14, 8:12 am, Erik Wikström <eri...@student.chalmers.sewrote:
On 14 Maj, 07:36, "dasca...@gmail.com" <dasca...@gmail.comwrote:
I was wondering, why is overloading operator. (period) forbidden? It
would make a few odd applications possible (dynamic inheritance and
transparent remote method invocation spring to my mind) and it would
be fairly generic. The only sidecase I can see is that operator.
itself would not be looked up through operator. .
I read that there was previous debate on the subject, but I haven't
been able to find why it was rejected.
There are two reasons against it that I know of, the first being that
the general consensus in the standard committee is that it might
introduce too much trouble* , since a user would never know what will
happen if he uses the .-operator,
By that standard, you couldn't overload any of the operators.
the second is the problem you
mentioned about how to implement the normal behaviour.
The answer to that is also well known: (&obj)->.
And if operator-is overloaded?
As Sylvester has already indicated, the result of &obj is
normally a pointer, and you cannot overload on a pointer. More
generally, you're the author of the class. If the class is a
smart pointer, you overload ->, and not .; if the class is a
smart reference, you overload ., and not ->. And if the class
is designed to confuse the reader, you overload both.

There have been serious proposals concerning this before. I've
forgotten the issues now, but if I recall correctly, there was
one serious point which would have to be addressed. But it is
definitly doable, and extremely useful. (Logically, I'd say
that it would have to be a member function. But then, I'd say
that about operator& as well.)
I know of no
convincing reason for allowing it since it really would not enable
behaviour that can't be simulated in other ways.
Smart references and proxies.
* In fact a recent proposal (n2200) suggests that all operators,
except . (member selection), :: (namespace) and .* (memberpointer),
should be overloadable both as members and non member (that includes
new and delete).
Hmmm. I'll have to look at it, but I'd be very surprised if
they allow the assignment operator to be a non-member. That
would cause no end of problems. (I'm also curious with regards
to ?:. I don't think I'd like to see it overloadable unless
there was a requirement that the first argument be a user
defined type.)
operator= was one of them specifically mentioned, (along with *_cast)
but I think that I was too quick when I said all operators, I don't
thin ?: was included.
I looked at it. Operator= is mentionned, but anything which
could be a copy assignment is still required to be a member.
You can only do things like operator=( MyClass&, int ) as
non-members.

The proposal is far from being uninteresting. But it is also
rather far from being "ready", and I doubt that there is enough
time left to get it ready before the next version of the
standard.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 15 '07 #10
<da******@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
I was wondering, why is overloading operator. (period) forbidden?
Because no one ever brought a coherent, workable proposal to the standards
committee for how the feature should be defined. There was one person--I
forget his name--who made such a proposal on Usenet, but whenever anyone
tried to ask questions about potential problems with his proposal, he became
angry. This anger discouraged others from pursuing the issue.
May 16 '07 #11
On May 16, 3:59 pm, "Andrew Koenig" <a...@acm.orgwrote:
<dasca...@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
I was wondering, why is overloading operator. (period) forbidden?
Because no one ever brought a coherent, workable proposal to
the standards committee for how the feature should be defined.
There was one person--I forget his name--who made such a
proposal on Usenet, but whenever anyone tried to ask questions
about potential problems with his proposal, he became angry.
This anger discouraged others from pursuing the issue.
IIRC, he did make a formal proposal. As you said, however,
there was a personality clash, with the result that the
discussion generated more heat than light, and the concrete
issues didn't get addressed.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 16 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: victor75040 | last post by:
Before you all start flaming me, I am not a student and this is not for any homework. Just someone learing c++ on their own. I am now up to the chapter in my book that describes operator...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
20
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==, !=, >=, and <= operators using bool. I am having...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
19
by: Jess | last post by:
Hello, After seeing some examples about operator overloading, I'm still a bit confused about the general syntax. The following is what I think, not sure whether it's correct. 1. For a unary...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
30
by: none | last post by:
I'm trying to overload the = operator using templates, but I have some problems with one of the overloads, I would like to make something like that: intvariable = fooclass; here's a pseudo...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.