Jump to page: 1 25  
Page
Thread overview
Reverse Operator Overloading
Aug 17, 2002
Walter
Aug 17, 2002
Burton Radons
Aug 17, 2002
Jason Mills
Aug 17, 2002
Walter
Aug 18, 2002
anderson
Aug 18, 2002
Walter
Aug 18, 2002
anderson
Aug 18, 2002
Sean L. Palmer
Aug 17, 2002
Pavel Minayev
Aug 17, 2002
Walter
Aug 17, 2002
anderson
Aug 17, 2002
anderson
Aug 17, 2002
Walter
Aug 17, 2002
anderson
Aug 17, 2002
anderson
Aug 17, 2002
anderson
Aug 18, 2002
Sean L. Palmer
Aug 18, 2002
Walter
Aug 18, 2002
Pavel Minayev
Aug 18, 2002
Martin M. Pedersen
Aug 18, 2002
Walter
Aug 19, 2002
Sean L. Palmer
Aug 19, 2002
Walter
Aug 20, 2002
Martin M. Pedersen
Aug 20, 2002
Pavel Minayev
Aug 20, 2002
Martin M. Pedersen
Aug 20, 2002
Richard Krehbiel
Aug 20, 2002
Martin M. Pedersen
Aug 21, 2002
Richard Krehbiel
Aug 20, 2002
Pavel Minayev
Aug 20, 2002
Sean L. Palmer
Aug 19, 2002
anderson
Aug 19, 2002
Russ Lewis
Aug 19, 2002
Walter
Aug 19, 2002
Russ Lewis
Aug 19, 2002
Walter
Aug 20, 2002
anderson
Aug 20, 2002
Russell Lewis
Aug 20, 2002
Walter
Aug 20, 2002
Burton Radons
Aug 20, 2002
Walter
Aug 17, 2002
Patrick Down
Aug 18, 2002
Walter
Aug 19, 2002
anderson
Re: Reverse Operator Overloading Question (division)
Aug 19, 2002
anderson
Aug 19, 2002
anderson
August 17, 2002
I'm looking at the overloading of commutative operators add, multiply, etc. There are a couple of options for dealing with the (1 + a) case:

1) Provide a separate "add_r" function to make a.add_r(1)
2) Allow the compiler to swap the operands and call a.add(1)
3) Allow the compiler to swap the operands unless there is an "add_r"
function defined.

I'm leaning towards (2), it is simpler and in keeping with the way the
builtin operators work. Note that this would only apply to those operators
that are mathematically commutative. It does not allow associative
rearrangement, and does not allow replacing (a - b) with (a + (-b)), etc.


August 17, 2002
Walter wrote:

> I'm looking at the overloading of commutative operators add, multiply, etc.
> There are a couple of options for dealing with the (1 + a) case:
> 
> 1) Provide a separate "add_r" function to make a.add_r(1)
> 2) Allow the compiler to swap the operands and call a.add(1)
> 3) Allow the compiler to swap the operands unless there is an "add_r"
> function defined.
> 
> I'm leaning towards (2), it is simpler and in keeping with the way the
> builtin operators work. Note that this would only apply to those operators
> that are mathematically commutative. It does not allow associative
> rearrangement, and does not allow replacing (a - b) with (a + (-b)), etc.

It's not really worth the gain when you can write out a basic reverse with one line:

    Vector operator (extended a) + (Vector b) { return b + a; }
    Vector operator (extended a) - (Vector b) { return -b + a; }
    Vector operator (extended a) * (Vector b) { return b * a; }

IMO it's clearer to have every permutation written out and doesn't interfere with getting anything done.  It's not worth the special case effort for the two operators which can sometimes sorta be automatically reversed.

August 17, 2002
On Fri, 16 Aug 2002 17:54:54 -0700 "Walter" <walter@digitalmars.com> wrote:

> I'm leaning towards (2), it is simpler and in keeping with the way the
> builtin operators work. Note that this would only apply to those operators
> that are mathematically commutative. It does not allow associative
> rearrangement, and does not allow replacing (a - b) with (a + (-b)), etc.

And are there any operators that are _always_ commutative? I might be wrong,
but I remember some kind of numbers in math for which + is not commutative...

IMHO it's better to define operators separately for every case.
August 17, 2002
A major point for using overloaded operators is for matrix/vector type operations which contain things which are not commutative. I'd say either 1 or 3. Or 1 and 2 (providing a both operation).


"Walter" <walter@digitalmars.com> wrote in message news:ajk6mq$2o08$1@digitaldaemon.com...
> I'm looking at the overloading of commutative operators add, multiply,
etc.
> There are a couple of options for dealing with the (1 + a) case:
>
> 1) Provide a separate "add_r" function to make a.add_r(1)
> 2) Allow the compiler to swap the operands and call a.add(1)
> 3) Allow the compiler to swap the operands unless there is an "add_r"
> function defined.
>
> I'm leaning towards (2), it is simpler and in keeping with the way the
> builtin operators work. Note that this would only apply to those operators
> that are mathematically commutative. It does not allow associative
> rearrangement, and does not allow replacing (a - b) with (a + (-b)), etc.
>
>


August 17, 2002
"Pavel Minayev" <evilone@omen.ru> wrote in message news:CFN374854774504051@news.digitalmars.com...
> And are there any operators that are _always_ commutative? I might be
wrong,
> but I remember some kind of numbers in math for which + is not
commutative...

I can't think of any.

> IMHO it's better to define operators separately for every case.




August 17, 2002
"anderson" <anderson@firestar.com.au> wrote in message news:ajkvq6$h3d$1@digitaldaemon.com...
> A major point for using overloaded operators is for matrix/vector type operations which contain things which are not commutative. I'd say either
1
> or 3. Or 1 and 2 (providing a both operation).

The only time the commutativity is an issue is when the left operand is not a class object, i.e. is a scalar or array. Isn't matrix math commutative when adding/multiplying by a constant?


August 17, 2002
I agree with Pavel here, it should be standard across all operators. That way there's no confusion.

"Walter" <walter@digitalmars.com> wrote in message news:ajl03j$hj4$1@digitaldaemon.com...
>
> "Pavel Minayev" <evilone@omen.ru> wrote in message news:CFN374854774504051@news.digitalmars.com...
> > And are there any operators that are _always_ commutative? I might be
> wrong,
> > but I remember some kind of numbers in math for which + is not
> commutative...
>
> I can't think of any.
>
> > IMHO it's better to define operators separately for every case.
>
>
>
>


August 17, 2002
"Walter" <walter@digitalmars.com> wrote in message news:ajl2ei$106c$1@digitaldaemon.com...
>
> "anderson" <anderson@firestar.com.au> wrote in message news:ajkvq6$h3d$1@digitaldaemon.com...
> > A major point for using overloaded operators is for matrix/vector type operations which contain things which are not commutative. I'd say
either
> 1
> > or 3. Or 1 and 2 (providing a both operation).
>
> The only time the commutativity is an issue is when the left operand is
not
> a class object, i.e. is a scalar or array. Isn't matrix math commutative when adding/multiplying by a constant?
>

Your right. I suppose if there are any cases for non commutative you could define an integer as a class and get around it that way.


August 17, 2002
Come to think of it....

How will...

Matrix ** Integer

be handled?


"Walter" <walter@digitalmars.com> wrote in message news:ajl2ei$106c$1@digitaldaemon.com...
>
> "anderson" <anderson@firestar.com.au> wrote in message news:ajkvq6$h3d$1@digitaldaemon.com...
> > A major point for using overloaded operators is for matrix/vector type operations which contain things which are not commutative. I'd say
either
> 1
> > or 3. Or 1 and 2 (providing a both operation).
>
> The only time the commutativity is an issue is when the left operand is
not
> a class object, i.e. is a scalar or array. Isn't matrix math commutative when adding/multiplying by a constant?
>
>


August 17, 2002
"anderson" <anderson@firestar.com.au> wrote in message news:ajl71t$14of$1@digitaldaemon.com...
> Come to think of it....
>
> How will...
>
> Matrix ** Integer
  Integer ** Matrix
>
> be handled?
>
>
> "Walter" <walter@digitalmars.com> wrote in message news:ajl2ei$106c$1@digitaldaemon.com...
> >
> > "anderson" <anderson@firestar.com.au> wrote in message news:ajkvq6$h3d$1@digitaldaemon.com...
> > > A major point for using overloaded operators is for matrix/vector type operations which contain things which are not commutative. I'd say
> either
> > 1
> > > or 3. Or 1 and 2 (providing a both operation).
> >
> > The only time the commutativity is an issue is when the left operand is
> not
> > a class object, i.e. is a scalar or array. Isn't matrix math commutative when adding/multiplying by a constant?
> >
> >
>
>


« First   ‹ Prev
1 2 3 4 5