Thread View: comp.std.c++
4 messages
4 total messages
Started by Karl Keyte
Fri, 11 Sep 1992 14:42
Re: Answers on a postcard
Author: Karl Keyte
Date: Fri, 11 Sep 1992 14:42
Date: Fri, 11 Sep 1992 14:42
49 lines
2183 bytes
2183 bytes
...from a posting on 'comp.lang.c++'
In article <1992Sep10.174914.838@taumet.com>, steve@taumet.com (Steve Clamage)
says:
>
>Karl Keyte <KKEYTE@ESOC.BITNET> writes:
>
>|#include <iostream.h>
>
>|class X {
>| private: int value;
>| public: int operator , (int i) { return i+10; };
>| int operator + (int i) { return value+i; };
>| X & operator = (int i) { value=i; return *this; };
>|};
>
>
>|void main()
>|{
>| X x;
>
>| cout << "Value is " << (x=1, x+1) << endl;
>|}
>
>In the expression in parens, it is unspecified whether "x=1" or
>"x+1" is evaluated first. They are arguments to the "operator,()"
>function, not the left and right side of a comma-expression. If
>"x+1" is evaluated first, which is legal, it will use an
>uninitialized "x.value".
>
>Don't write code which depends on unspecified order of evaluation.
>
>If there were no "op,()", the "," would mark a comma-expression,
>and the "x=1" would have to be evaluated first; in that case the
>whole expression would be well-defined.
The implication of what you say is that it is not possible to overload
operators maintaining the correct functionality of the operator. This
indeed means that precedence in overloaded operators cannot be maintained
if all operands are simply passed as arguments to the function implementing
the operator functionality and evaluated in a language independent order.
Imagine commonly used "chain" operators such as && and ΒΊΒΊ.
If what is described above is correct, and Mr.Clamage clearly has much
experience in the area, then I think we have justification for pressing
for a change in this area. If not, I would ask: How does one overload
the comma operator while maintaining its attibutes as an operator?
Karl
Re: Answers on a postcard
Author: Tom O Breton
Date: Sat, 12 Sep 1992 17:19
Date: Sat, 12 Sep 1992 17:19
25 lines
977 bytes
977 bytes
Karl:
> If not, I would ask: How does one overload the comma operator while
> maintaining its attibutes as an operator?
It's of dubious usefulness but it can be done:
class thisclass
{ int& operator,(int a); }
//To call this: (thisclass)X, (int)Y ... (outside of arg-lists, of course)
This will return you an int, just as if the default comma operator had
executed.
In general, chain operators by returning references (Usually to the same
argument they took).
Tom
--
The Tom spreads its huge, scaly wings and soars into the wild sky...
up... up... out of sight... (tob@world.std.com)
Re: Answers on a postcard
Author: Markku Sakkinen
Date: Mon, 14 Sep 1992 08:34
Date: Mon, 14 Sep 1992 08:34
41 lines
2012 bytes
2012 bytes
In article <BuH6sK.54r@world.std.com> tob@world.std.com (Tom O Breton) writes:
>Karl:
>
>> If not, I would ask: How does one overload the comma operator while
>> maintaining its attibutes as an operator?
>
>
>It's of dubious usefulness but it can be done:
>
>class thisclass
>{ int& operator,(int a); }
>
>//To call this: (thisclass)X, (int)Y ... (outside of arg-lists, of course)
>
>This will return you an int, just as if the default comma operator had
>executed.
> ...
Sorry, but you did not see what the problem is.
With an overloaded comma operator it is impossible to make sure
that the first operand (X) is _evaluated_ (completely) before
the second one, as is the case with the built-in operator.
Indeed, one would probably often like not to have the second operator
evaluated yet at all before the operator function is invoked.
This is important if either of the operands is an expression
with side effects (and with the built-in comma, you would not use
it at all unless the first operand really has side effects).
BTW, I regard it as a mistake to make comma (and '&') overloadable at all.
It is a clear breach of the principle: "C++ was designed to be
extensible, but not mutable".
----------------------------------------------------------------------
Markku Sakkinen (sakkinen@jytko.jyu.fi)
SAKKINEN@FINJYU.bitnet (alternative network address)
Department of Computer Science and Information Systems
University of Jyvaskyla (a's with umlauts)
PL 35
SF-40351 Jyvaskyla (umlauts again)
Finland
----------------------------------------------------------------------
Re: Answers on a postcard
Author: Daniel R. Edelson
Date: Mon, 14 Sep 1992 09:00
Date: Mon, 14 Sep 1992 09:00
28 lines
1890 bytes
1890 bytes
In article <92255.154216KKEYTE@ESOC.BITNET>, KKEYTE@ESOC.BITNET (Karl Keyte) writes: |> ...from a posting on 'comp.lang.c++' |> |> In article <1992Sep10.174914.838@taumet.com>, steve@taumet.com (Steve Clamage) |> says: |> > |> The implication of what you say is that it is not possible to overload |> operators maintaining the correct functionality of the operator. This |> indeed means that precedence in overloaded operators cannot be maintained |> if all operands are simply passed as arguments to the function implementing |> the operator functionality and evaluated in a language independent order. |> Imagine commonly used "chain" operators such as && and ::. |> |> If what is described above is correct, and Mr.Clamage clearly has much |> experience in the area, then I think we have justification for pressing |> for a change in this area. If not, I would ask: How does one overload |> the comma operator while maintaining its attibutes as an operator? |> |> Karl It seems to me that the ability to look at a comma expression and infer some semantics from the syntax is a valuable aid for program readability. This seems to me like an argument for not permitting the comma operator to be overloaded. Of course, the logical extension is to then forbid && and || from being overloaded as well, but this seems unlikely to happen, unfortunately. Daniel.Edelson@inria.fr
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads