Operators that cannot be Overloaded - WHY?

Discussion in 'C++' started by Brad Eck, Oct 27, 2004.

  1. Brad Eck

    Brad Eck Guest

    "The only operators that cannot be overloaded
    are :: (scope resolution), . (member selection), and .* (member
    selection through pointer to function). Quoting from Stroustrup's 3rd
    edition of _The C++ Programming Language_, section 11.2 (page 263),
    these three operators 'take a name, rather than a value, as their
    second operand and provide the primary means of referring to members.
    Allowing them to be overloaded would lead to subtleties.'"

    First, I've seen ?: and sizeof added to this list. Someone also
    mentioned 'delete' but Stroustrup doesn't refer to it as one. In fact,
    it's in the list of overloadable operators.

    Second, what is meant by 'take a name'? What is meant by 'subtleties'?
    Any thoughts as to the detailed reasoning behind these?
    Brad Eck, Oct 27, 2004
    #1
    1. Advertising

  2. "Brad Eck" <> wrote...
    > "The only operators that cannot be overloaded
    > are :: (scope resolution), . (member selection), and .* (member
    > selection through pointer to function). Quoting from Stroustrup's 3rd
    > edition of _The C++ Programming Language_, section 11.2 (page 263),
    > these three operators 'take a name, rather than a value, as their
    > second operand and provide the primary means of referring to members.
    > Allowing them to be overloaded would lead to subtleties.'"
    >
    > First, I've seen ?: and sizeof added to this list.


    The ternary op is excluded probably due to complexity of its relation
    to the assignment op. As to sizeof, ::, they're not really operators
    in the true sense, I guess.

    > Someone also
    > mentioned 'delete' but Stroustrup doesn't refer to it as one. In fact,
    > it's in the list of overloadable operators.


    'delete' can be overloaded. Whoever mentioned it probably didn't know
    what he/she was talking about.

    > Second, what is meant by 'take a name'? What is meant by 'subtleties'?


    Well, if you overload the . (member access) then the only way to access
    a member would be (&obj)->member. Awkward. Now, if both '&' and -> are
    overloaded, what do you get?...

    > Any thoughts as to the detailed reasoning behind these?


    Detailed, 'fraid not. Try "Design and Evolution of C++". There is
    a whole chapter on overloading.

    V
    Victor Bazarov, Oct 27, 2004
    #2
    1. Advertising

  3. "Brad Eck" <> wrote in message
    news:...
    > "The only operators that cannot be overloaded
    > are :: (scope resolution), . (member selection), and .* (member
    > selection through pointer to function). Quoting from Stroustrup's 3rd
    > edition of _The C++ Programming Language_, section 11.2 (page 263),
    > these three operators 'take a name, rather than a value, as their
    > second operand and provide the primary means of referring to members.
    > Allowing them to be overloaded would lead to subtleties.'"
    >
    > First, I've seen ?: and sizeof added to this list. Someone also
    > mentioned 'delete' but Stroustrup doesn't refer to it as one. In fact,
    > it's in the list of overloadable operators.
    >
    > Second, what is meant by 'take a name'? What is meant by 'subtleties'?
    > Any thoughts as to the detailed reasoning behind these?


    Most normal operator take values, e.g. operator+ might add two integer
    values. But the right hand side of operators . .* and :: are names of
    things, e.g. name of a class member. Left hand side of :: is a name as well.
    Difference between a name and a value seems pretty clear to me, and I guess
    the subtleties would be the fact that nowhere else in C++ are you allow to
    manipulate the names of class members, variables etc.

    john
    John Harrison, Oct 27, 2004
    #3
  4. Brad Eck

    Ron Natalie Guest

    Victor Bazarov wrote:

    > 'delete' can be overloaded. Whoever mentioned it probably didn't know
    > what he/she was talking about.


    Delete CANNOT be loaded. It's just syntactic stupidity that makes the
    deallocation function called "operator delete".
    Ron Natalie, Oct 27, 2004
    #4
  5. Brad Eck

    Ron Natalie Guest

    John Harrison wrote:

    > Most normal operator take values, e.g. operator+ might add two integer
    > values. But the right hand side of operators . .* and :: are names of
    > things, e.g. name of a class member.


    operator-> works with a member on the rhs.
    Ron Natalie, Oct 27, 2004
    #5
  6. "Ron Natalie" <> wrote in message
    news:417f81ca$0$28236$...
    > John Harrison wrote:
    >
    > > Most normal operator take values, e.g. operator+ might add two integer
    > > values. But the right hand side of operators . .* and :: are names of
    > > things, e.g. name of a class member.

    >
    > operator-> works with a member on the rhs.


    Use but in terms of how it is overloaded, operator-> is a unary operator
    which returns a pointer, to which operator-> is reapplied.

    Now Bjarne could have made it that operator. is overloaded as a unary
    operator which returns a reference to which operator. is then reapplied. He
    didn't and I don't know the reason but it seems like a reasonable decision
    to me.

    john
    John Harrison, Oct 27, 2004
    #6
  7. John Harrison wrote:
    > "Ron Natalie" <> wrote in message
    > news:417f81ca$0$28236$...
    >
    >>John Harrison wrote:
    >>
    >>
    >>>Most normal operator take values, e.g. operator+ might add two integer
    >>>values. But the right hand side of operators . .* and :: are names of
    >>>things, e.g. name of a class member.

    >>
    >>operator-> works with a member on the rhs.

    >
    >
    > Use but in terms of how it is overloaded, operator-> is a unary operator
    > which returns a pointer, to which operator-> is reapplied.
    >
    > Now Bjarne could have made it that operator. is overloaded as a unary
    > operator which returns a reference to which operator. is then reapplied. He
    > didn't and I don't know the reason but it seems like a reasonable decision
    > to me.


    class SomeClass {};

    class HasArrowOverloaded {
    public:
    SomeClass* operator->();
    };

    class HasDotOverloaded {
    HasArrowOverloaded& operator &();
    SomeClass& operator .(); // impossible in current C++, but
    // let's pretend it's OK
    void foo();
    };

    int main() {
    HasDotOverloaded hdo;
    // I want to call 'foo' for 'hdo'. How?
    }

    V
    Victor Bazarov, Oct 27, 2004
    #7
  8. Ron Natalie wrote:
    > Victor Bazarov wrote:
    >
    >> 'delete' can be overloaded. Whoever mentioned it probably didn't know
    >> what he/she was talking about.

    >
    >
    > Delete CANNOT be loaded. It's just syntactic stupidity that makes the
    > deallocation function called "operator delete".


    Whatever you call that ("syntactic stupidity" or "semantic brilliance")
    you still can do

    class RonDoesntLikeMeBooHoo {
    public:
    void operator delete(void*, size_t);
    };

    and when you say

    RonDoesntLikeMeBooHoo *foo;
    ...
    delete foo;

    the _overloaded_ function will be called. And it has the word "operator"
    in its name. If that means that it "CANNOT be loaded", then I'm the last
    Chinese emperor.

    V
    Victor Bazarov, Oct 27, 2004
    #8
  9. Victor Bazarov wrote:

    > John Harrison wrote:
    >
    >> "Ron Natalie" <> wrote in message
    >> news:417f81ca$0$28236$...
    >>
    >>> John Harrison wrote:
    >>>
    >>>
    >>>> Most normal operator take values, e.g. operator+ might add two integer
    >>>> values. But the right hand side of operators . .* and :: are names of
    >>>> things, e.g. name of a class member.
    >>>
    >>>
    >>> operator-> works with a member on the rhs.

    >>
    >>
    >>
    >> Use but in terms of how it is overloaded, operator-> is a unary operator
    >> which returns a pointer, to which operator-> is reapplied.
    >>
    >> Now Bjarne could have made it that operator. is overloaded as a unary
    >> operator which returns a reference to which operator. is then
    >> reapplied. He
    >> didn't and I don't know the reason but it seems like a reasonable
    >> decision
    >> to me.

    >
    >
    > class SomeClass {};
    >
    > class HasArrowOverloaded {
    > public:
    > SomeClass* operator->();
    > };
    >
    > class HasDotOverloaded {
    > HasArrowOverloaded& operator &();
    > SomeClass& operator .(); // impossible in current C++, but
    > // let's pretend it's OK
    > void foo();
    > };
    >
    > int main() {
    > HasDotOverloaded hdo;
    > // I want to call 'foo' for 'hdo'. How?
    > }
    >
    > V

    hdo.HasDotOverloaded::foo() ?

    --
    Regards,
    Slava
    Vyacheslav Kononenko, Oct 27, 2004
    #9
  10. Brad Eck

    JKop Guest

    Victor Bazarov posted:

    > Ron Natalie wrote:
    >> Victor Bazarov wrote:
    >>
    >>> 'delete' can be overloaded. Whoever mentioned it probably didn't

    know
    >>> what he/she was talking about.

    >>
    >>
    >> Delete CANNOT be loaded. It's just syntactic stupidity that makes

    the
    >> deallocation function called "operator delete".

    >
    > Whatever you call that ("syntactic stupidity" or "semantic

    brilliance")
    > you still can do
    >
    > class RonDoesntLikeMeBooHoo {
    > public:
    > void operator delete(void*, size_t);
    > };
    >
    > and when you say
    >
    > RonDoesntLikeMeBooHoo *foo;
    > ...
    > delete foo;
    >
    > the _overloaded_ function will be called. And it has the word

    "operator"
    > in its name. If that means that it "CANNOT be loaded", then I'm the

    last
    > Chinese emperor.
    >
    > V



    Now that's the kind of ridiculing debate I like to see!

    Handbags at dawn, ladies!


    -JKop
    JKop, Oct 27, 2004
    #10
  11. Vyacheslav Kononenko wrote:

    > Victor Bazarov wrote:
    >
    >> John Harrison wrote:
    >>
    >>> "Ron Natalie" <> wrote in message
    >>> news:417f81ca$0$28236$...
    >>>
    >>>> John Harrison wrote:
    >>>>
    >>>>
    >>>>> Most normal operator take values, e.g. operator+ might add two integer
    >>>>> values. But the right hand side of operators . .* and :: are names of
    >>>>> things, e.g. name of a class member.
    >>>>
    >>>>
    >>>>
    >>>> operator-> works with a member on the rhs.
    >>>
    >>>
    >>>
    >>>
    >>> Use but in terms of how it is overloaded, operator-> is a unary operator
    >>> which returns a pointer, to which operator-> is reapplied.
    >>>
    >>> Now Bjarne could have made it that operator. is overloaded as a unary
    >>> operator which returns a reference to which operator. is then
    >>> reapplied. He
    >>> didn't and I don't know the reason but it seems like a reasonable
    >>> decision
    >>> to me.

    >>
    >>
    >>
    >> class SomeClass {};
    >>
    >> class HasArrowOverloaded {
    >> public:
    >> SomeClass* operator->();
    >> };
    >>
    >> class HasDotOverloaded {
    >> HasArrowOverloaded& operator &();
    >> SomeClass& operator .(); // impossible in current C++, but
    >> // let's pretend it's OK
    >> void foo();
    >> };
    >>
    >> int main() {
    >> HasDotOverloaded hdo;
    >> // I want to call 'foo' for 'hdo'. How?
    >> }
    >>
    >> V

    >
    > hdo.HasDotOverloaded::foo() ?


    If analogy with operator-> is considered, then the _right_ side of that
    operator has no affect on the result.

    Generally speaking, I don't know. Shouldn't it actually say "SomeClass
    has no 'HasDotOverloaded' member" or something? But let's simply make
    it a bit more interesting. Consider the to of the code above corrected
    as following:

    struct Base {
    virtual void foo();
    };

    class SomeClass : public Base {
    void foo();
    };

    Now, how sould compiler decide what to call if the syntax is

    hdo.Base::foo(); // want to call Base::foo for SomeClass&

    or

    hdo.foo(); // which 'foo'?

    See the problem? My post is not intended to spark a discussion that has
    a goal to figure out how to overload operator dot correctly. I am just
    trying to illustrate the problems that arise if such attempt is made.

    V
    Victor Bazarov, Oct 27, 2004
    #11
  12. Brad Eck

    Ron Natalie Guest

    Victor Bazarov wrote:

    > RonDoesntLikeMeBooHoo *foo;
    > ...
    > delete foo;
    >
    > the _overloaded_ function will be called. And it has the word "operator"
    > in its name. If that means that it "CANNOT be loaded", then I'm the last
    > Chinese emperor.
    >


    The overloaded fucntion gets called, but it isn't the implementation
    of the delete operator. The delete operator does a lot of other stuff
    (notably calling destructors) in addition to calling operator delete.
    Ron Natalie, Oct 27, 2004
    #12
  13. Brad Eck

    Ron Natalie Guest

    Victor Bazarov wrote:

    > If analogy with operator-> is considered, then the _right_ side of that
    > operator has no affect on the result.
    >

    By the way, I'm not arguing that operator. should be allowed to
    be overloading, I was just pointing out someone's claim that the
    fact that the rhs of the . operator was a member name was a reason
    (and I pointed out the counterexample).

    The main reason is that . would be too darn confusing if overloaded.
    You gotta start from somewhere. Frankly, if it were up to me,
    you wouldn't be able to overload unary & either.
    Ron Natalie, Oct 27, 2004
    #13
  14. Ron Natalie wrote:
    > Victor Bazarov wrote:
    >
    >> RonDoesntLikeMeBooHoo *foo;
    >> ...
    >> delete foo;
    >>
    >> the _overloaded_ function will be called. And it has the word "operator"
    >> in its name. If that means that it "CANNOT be loaded", then I'm the last
    >> Chinese emperor.
    >>

    >
    > The overloaded fucntion gets called, but it isn't the implementation
    > of the delete operator. The delete operator does a lot of other stuff
    > (notably calling destructors) in addition to calling operator delete.


    Yes, a good opportunity for a language lawyer to demonstrate the knowledge
    of the difference between the delete operator and the operator delete,
    isn't it? The memory allocation function ("operator delete") can be
    overloaded. That's the point. Are you disputing that? I guess, not.
    Victor Bazarov, Oct 27, 2004
    #14
  15. Brad Eck

    Alex Vinokur Guest

    "Victor Bazarov" <> wrote in message news:11Rfd.7248$09.us.to.verio.net...
    [snip]
    > Yes, a good opportunity for a language lawyer to demonstrate the knowledge
    > of the difference between the delete operator and the operator delete,
    > isn't it?

    [snip]

    Is there any good description of the difference between the delete operator and the operator delete?

    By the way, Google "delete operator" in www.parashift.com (Marshall Cline's C++ FAQs Lite) gives
    http://www.google.com/search?hl=en&lr=&c2coff=1&sitesearch=www.parashift.com&q="delete operator"
    -> "Your search - "delete operator" - did not match any documents"


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Oct 28, 2004
    #15
  16. Alex Vinokur wrote:
    > "Victor Bazarov" <> wrote in message news:11Rfd.7248$09.us.to.verio.net...
    > [snip]
    >
    >>Yes, a good opportunity for a language lawyer to demonstrate the knowledge
    >>of the difference between the delete operator and the operator delete,
    >>isn't it?

    >
    > [snip]
    >
    > Is there any good description of the difference between the delete operator and the operator delete?
    >
    > By the way, Google "delete operator" in www.parashift.com (Marshall Cline's C++ FAQs Lite) gives
    > http://www.google.com/search?hl=en&lr=&c2coff=1&sitesearch=www.parashift.com&q="delete operator"
    > -> "Your search - "delete operator" - did not match any documents"


    "Delete operator" is what is used when you delete a pointer in your code:

    delete ptr;

    "Operator delete" is the deallocation function that you can overload.

    The main difference is that "delete operator" invokes (calls) the proper
    destructor and then uses "operator delete" to free the memory.

    Same thing with "new operator" and "operator new".

    V
    Victor Bazarov, Oct 28, 2004
    #16
  17. Brad Eck

    Alex Vinokur Guest

    "Victor Bazarov" <> wrote in message news:gw6gd.7632$09.us.to.verio.net...
    > Alex Vinokur wrote:
    > > "Victor Bazarov" <> wrote in message news:11Rfd.7248$09.us.to.verio.net...
    > > [snip]
    > >
    > >>Yes, a good opportunity for a language lawyer to demonstrate the knowledge
    > >>of the difference between the delete operator and the operator delete,
    > >>isn't it?

    > >
    > > [snip]
    > >
    > > Is there any good description of the difference between the delete operator and the operator delete?
    > >
    > > By the way, Google "delete operator" in www.parashift.com (Marshall Cline's C++ FAQs Lite) gives
    > > http://www.google.com/search?hl=en&lr=&c2coff=1&sitesearch=www.parashift.com&q="delete operator"
    > > -> "Your search - "delete operator" - did not match any documents"

    >
    > "Delete operator" is what is used when you delete a pointer in your code:
    >
    > delete ptr;
    >
    > "Operator delete" is the deallocation function that you can overload.
    >
    > The main difference is that "delete operator" invokes (calls) the proper
    > destructor and then uses "operator delete" to free the memory.
    >
    > Same thing with "new operator" and "operator new".
    >
    > V


    Here is description og file <new> from
    Standard C++ Library Module Reference Guide from Rogue Wave web site:
    http://www.roguewave.com/support/docs/SourcePro/stdlibref/new-h.html

    Is "new" here "new operator" or "operator new"?
    Is "delete" here "delete operator" or "operator delete"?


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Oct 29, 2004
    #17
  18. Alex Vinokur wrote:
    > "Victor Bazarov" <> wrote in message news:gw6gd.7632$09.us.to.verio.net...
    >
    >>Alex Vinokur wrote:
    >>
    >>>"Victor Bazarov" <> wrote in message news:11Rfd.7248$09.us.to.verio.net...
    >>>[snip]
    >>>
    >>>
    >>>>Yes, a good opportunity for a language lawyer to demonstrate the knowledge
    >>>>of the difference between the delete operator and the operator delete,
    >>>>isn't it?
    >>>
    >>>[snip]
    >>>
    >>>Is there any good description of the difference between the delete operator and the operator delete?
    >>>
    >>>By the way, Google "delete operator" in www.parashift.com (Marshall Cline's C++ FAQs Lite) gives
    >>>http://www.google.com/search?hl=en&lr=&c2coff=1&sitesearch=www.parashift.com&q="delete operator"
    >>>-> "Your search - "delete operator" - did not match any documents"

    >>
    >>"Delete operator" is what is used when you delete a pointer in your code:
    >>
    >> delete ptr;
    >>
    >>"Operator delete" is the deallocation function that you can overload.
    >>
    >>The main difference is that "delete operator" invokes (calls) the proper
    >>destructor and then uses "operator delete" to free the memory.
    >>
    >>Same thing with "new operator" and "operator new".
    >>
    >>V

    >
    >
    > Here is description og file <new> from
    > Standard C++ Library Module Reference Guide from Rogue Wave web site:
    > http://www.roguewave.com/support/docs/SourcePro/stdlibref/new-h.html
    >
    > Is "new" here "new operator" or "operator new"?


    What do you think? What do you see there?

    In the following code there are both, can you spot them and tell them
    apart?

    class Blah {
    public:
    void* operator new(size_t);
    };

    int main() {
    Blah* blah = new Blah;
    }

    If not, the one in the class is "operator new" (doesn't it say so in
    the actual function declaration?), the one in the 'main' is the "new
    operator".

    > Is "delete" here "delete operator" or "operator delete"?


    Well, what do you think?

    OK, OK, any function declared using 'operator' and then some other symbol
    or word, is the "operator X" function. "operator delete" (when you see
    those words in that order in the code) is the deallocation function that
    is allowed to be overloaded for custom types and globally. "operator new"
    is the one declared in the <new> and one that you can overload in a class
    or globally.

    "New operator" is what is invoked by the "new expression".

    V
    Victor Bazarov, Oct 29, 2004
    #18
  19. Brad Eck

    Alex Vinokur Guest

    "Victor Bazarov" <> wrote in message news:Ztwgd.7712$09.us.to.verio.net...
    [snip]
    > class Blah {
    > public:

    -----------------------------------------
    This is declaration of "operator new"
    > void* operator new(size_t);

    -----------------------------------------
    > };


    This is definition of "operator new"
    void* Blah::eek:perator new(size_t)
    {
    // stuff
    }

    If we don't define our "operator new" then default "operator new" is used?

    >
    > int main() {

    --------------------------------------
    This is calling the "new operator".
    > Blah* blah = new Blah;

    --------------------------------------
    Can we directly call "operator new"?
    When is it worth doing that (instesd of calling the "new operator")?

    > }
    >
    > If not, the one in the class is "operator new" (doesn't it say so in
    > the actual function declaration?), the one in the 'main' is the "new
    > operator".

    [snip]


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Oct 29, 2004
    #19
  20. Alex Vinokur wrote:
    > "Victor Bazarov" <> wrote in message news:Ztwgd.7712$09.us.to.verio.net...
    > [snip]
    >
    >> class Blah {
    >> public:

    >
    > -----------------------------------------
    > This is declaration of "operator new"
    >
    >> void* operator new(size_t);

    >
    > -----------------------------------------
    >
    >> };

    >
    >
    > This is definition of "operator new"
    > void* Blah::eek:perator new(size_t)
    > {
    > // stuff
    > }
    >
    > If we don't define our "operator new" then default "operator new" is used?


    Right.

    >
    >
    >> int main() {

    >
    > --------------------------------------
    > This is calling the "new operator".
    >
    >> Blah* blah = new Blah;


    Basically. It's more appropriate to say "using new operator" instead of
    "calling" it.

    >
    > --------------------------------------
    > Can we directly call "operator new"?


    We can.

    > When is it worth doing that (instesd of calling the "new operator")?


    Probably never.

    V
    Victor Bazarov, Oct 29, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -Steve-
    Replies:
    2
    Views:
    360
    - Steve -
    Jul 28, 2003
  2. John Harrison
    Replies:
    5
    Views:
    321
    - Steve -
    Jul 29, 2003
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,733
    Smokey Grindel
    Dec 2, 2006
  4. Bangalore
    Replies:
    4
    Views:
    1,241
    Fabio Fracassi
    Jul 27, 2005
  5. Replies:
    14
    Views:
    1,311
    Girish Shetty
    Aug 12, 2005
Loading...

Share This Page