C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

synthesized operators in C++ - a good thing?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Gene Bushuyev
Guest





PostPosted: Thu Nov 03, 2005 1:04 pm    Post subject: synthesized operators in C++ - a good thing? Reply with quote



MS is adding sinthesized operators to Visual C++ (CLI?). Below is the
exceprt from the language specification I stumbled upon:
--------------------
The compound assignment operators (+=, -=, *=, /=, %=, >>=, <<=, ^=, &=, and
Quote:
=) are synthesized from other operators. For the expression x @= y (where @
denotes one of the operators listed above): If lookup for operator@=

succeeds, the rules specified so far are applied. Otherwise, the expression
x @= y is rewritten as x = x @ y, and the transformed expression is
interpreted with the rules specified so far. Identify when synthesis would
and would not occur. [[#56]]

If no overload for operator@= applies after overload resolution or
synthesis, the program is ill-formed.

Synthesis shall not occur for operators defined inside native classes.
---------------------

I hope that the last line means that normal C++ classes will not be affected
by this mis-feature. I think it rarely (if ever) makes sense to define @=
operators in terms of @ operators. The opposite can be the case. If MS adds
those rules into their C++ compiler, there will be a need for another rule
in the coding standards: beware operator synthesis. Am I overreacting? Can
it be a good feature after all?

-- Gene Bushuyev ~ Cadence Design Systems


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Valentin.Samko
Guest





PostPosted: Thu Nov 03, 2005 7:14 pm    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote



Quote:
I hope that the last line means that normal C++ classes will not be affected
by this mis-feature. I think it rarely (if ever) makes sense to define @=
operators in terms of @ operators.

This does not make much sense to me either. The opposite would probably
make sense (defining @ in terms of @+) but I still find this confusing.

Also, this feature will probably lead to more problems when developers
overuse operators.

Quote:
The opposite can be the case. If MS adds
those rules into their C++ compiler, there will be a need for another rule
in the coding standards: beware operator synthesis.

Yes, as they may lead to performance problems when someone forgets to
implement the correct operator (or even worse, if the correct operator
is implemented, but is not found when the name lookup is performed). I
would rather prefer to get a compile time error in such a case. Think
of MyStringClass '+=' operator implemented in terms of the '+'
operator.

Quote:
Am I overreacting? Can it be a good feature after all?

Well, may be they may be usefull for "immutable" types, where you never
modify your object (i.e. you throw away the old value set the new one).

--

Valentin Samko - http://www.valentinsamko.com


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gabriel Dos Reis
Guest





PostPosted: Thu Nov 03, 2005 7:34 pm    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote



"Gene Bushuyev" <blackhole (AT) spamguard (DOT) com> writes:

[...]

Quote:
I hope that the last line means that normal C++ classes will not be affected
by this mis-feature. I think it rarely (if ever) makes sense to define @=
operators in terms of @ operators.

There are few cases like adding or multiplying polynomials (or like)
where it makes sense. But as a whole, I believe it is a misfeature if
done mechanically with no user control/input.

--
Gabriel Dos Reis
[email]gdr (AT) integrable-solutions (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Mirek Fidler
Guest





PostPosted: Thu Nov 03, 2005 7:35 pm    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote

Gene Bushuyev wrote:
Quote:
MS is adding sinthesized operators to Visual C++ (CLI?). Below is the
exceprt from the language specification I stumbled upon:
--------------------
The compound assignment operators (+=, -=, *=, /=, %=, >>=, <<=, ^=, &=, and
|=) are synthesized from other operators. For the expression x @= y (where @
denotes one of the operators listed above): If lookup for operator@=
succeeds, the rules specified so far are applied. Otherwise, the expression
x @= y is rewritten as x = x @ y, and the transformed expression is
interpreted with the rules specified so far. Identify when synthesis would
and would not occur. [[#56]]

If no overload for operator@= applies after overload resolution or
synthesis, the program is ill-formed.

Synthesis shall not occur for operators defined inside native classes.
---------------------

I hope that the last line means that normal C++ classes will not be affected
by this mis-feature. I think it rarely (if ever) makes sense to define @=
operators in terms of @ operators. The opposite can be the case. If MS adds
those rules into their C++ compiler, there will be a need for another rule
in the coding standards: beware operator synthesis. Am I overreacting? Can
it be a good feature after all?

No. It is horrible. The reason not to buy the next MS compiler.

Mirek

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Tony Delroy
Guest





PostPosted: Thu Nov 03, 2005 7:36 pm    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote

I think there's some sense behind this, but share your concerns, and
think any facility should be explicitly controlled. Plenty of concise
but readable notations could be envisaged, such as:

auto X::operator+=, -=, *=, /=, %=;

Tony


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze
Guest





PostPosted: Fri Nov 04, 2005 11:11 am    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote

Gene Bushuyev wrote:
Quote:
MS is adding sinthesized operators to Visual C++ (CLI?). Below
is the exceprt from the language specification I stumbled
upon:
--------------------
The compound assignment operators (+=, -=, *=, /=, %=, >>=,
=, ^=, &=, and |=) are synthesized from other operators. For
the expression x @= y (where @ denotes one of the operators
listed above): If lookup for operator@= succeeds, the rules
specified so far are applied. Otherwise, the expression x @= y
is rewritten as x = x @ y, and the transformed expression is
interpreted with the rules specified so far. Identify when
synthesis would and would not occur. [[#56]]

If no overload for operator@= applies after overload
resolution or synthesis, the program is ill-formed.

Synthesis shall not occur for operators defined inside native
classes.
---------------------

I hope that the last line means that normal C++ classes will
not be affected by this mis-feature. I think it rarely (if
ever) makes sense to define @= operators in terms of @
operators. The opposite can be the case. If MS adds those
rules into their C++ compiler, there will be a need for
another rule in the coding standards: beware operator
synthesis. Am I overreacting? Can it be a good feature after
all?

I don't see any real reason to call it a feature -- I don't
think I've ever had a case where @= was derived from @, so its
usefulness is extremely limited. On the other hand, I don't see
any reason to be worried about it: IMHO, any code which defines
and operator @ and does not define @= is already broken, at
least at the design level. And if I understand correctly, if
there is a @=, the compiler uses it, and doesn't synthesize. So
the total effect of the "feature" is 0 (except maybe that it
took developer time which could have been spent on something
userful) -- it has absolutly no effect in reasonably designed
and written code.

--
James Kanze GABI Software
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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Daniel Krügler
Guest





PostPosted: Sat Nov 05, 2005 11:38 am    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote

kanze wrote:
Quote:
I don't see any real reason to call it a feature -- I don't
think I've ever had a case where @= was derived from @, so its
usefulness is extremely limited.

The only real-world example for defining @= in terms of @
I'm aware of is operator overloading of enumeration types. In this
case there is no alternative (I think) to first define @ using
a cast to a reasonable integral type (e.g. the unsigned form of the
promotion type) and after that defining @= in terms of the first.

Returning to the OP's question: I can't see convincing reasons for
the decision of this kind of autogenerated operators. Explicit
compiler advice to do so as in Glassborow's proposal, e.g.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1702.pdf
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1717.pdf

makes much more sense.

Greetings from Bremen,

Daniel

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Francis Glassborow
Guest





PostPosted: Sat Nov 05, 2005 2:29 pm    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote

In article <Rudaf.7607$BZ5.41 (AT) newssvr13 (DOT) news.prodigy.com>, Gene Bushuyev
<blackhole (AT) spamguard (DOT) com> writes
Quote:
I hope that the last line means that normal C++ classes will not be
affected
by this mis-feature. I think it rarely (if ever) makes sense to
define @=
operators in terms of @ operators. The opposite can be the case. If
MS adds
those rules into their C++ compiler, there will be a need for
another rule
in the coding standards: beware operator synthesis. Am I
overreacting? Can
it be a good feature after all?

It takes some companies a long time to learn the lesson that generated
code causes problems (if we do not want it we have to find a way to turn
it off)

As most experienced C++ programmers know, the @= operators are actually
more primitive than the corresponding @ ones. It is easy to automate
generation of @ from @= and so, in general, I would define the @= and
delegate to that to implement @.

It seems to me that MS is often motivated by making things easier for
poor quality programmers that make up a proportion (probably
substantial) of its customer base.

I find it difficult to see how a well designed user type could benefit
from this miss-feature. However let me turn my attention to possible
problems.

Consider:

mytype & operator+(int, mytype const &);

How do they propose to synthesize an operator += from that? Does the
return type have significance? The order of the parameters? Or what?

The trouble is that once a compiler provides such a 'feature' it will be
almost impossible to remove it without howls of agony from some
customers.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/
youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kwikius
Guest





PostPosted: Sat Nov 05, 2005 6:37 pm    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote


kanze wrote:

Quote:
and operator @ and does not define @= is already broken, at
least at the design level.

FWIW In a physical-quantities library, where a and b are
dimensioned-quantities, a * b is a valid operation , but a *= b
violates dimensional analysis rules.
see
http://www.servocomm.freeserve.co.uk/Cpp/physical_quantity/index.html

regards
Andy Little


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Francis Glassborow
Guest





PostPosted: Sat Nov 05, 2005 6:38 pm    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote

In article <436b5029$0$303$4d4ebb8e (AT) businessnews (DOT) de.uu.net>, Daniel
Krügler <dsp (AT) bdal (DOT) de> writes
Quote:
The only real-world example for defining @= in terms of @
I'm aware of is operator overloading of enumeration types.


What is wrong with:

enum type { /* enumerator list */ };

type& operator +=(type & t, int i){
return t = type (t + i);
}

Note that the + is just a builtin operator+(int, int).

Yes you could define operator+(type const &, int i) first if you want
but there is no need to.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Mirek Fidler
Guest





PostPosted: Sun Nov 06, 2005 12:11 pm    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote

Quote:
any reason to be worried about it: IMHO, any code which defines
and operator @ and does not define @= is already broken, at
least at the design level.

Like streams and operator<< ? :)

Mirek

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
jimmaureenrogers@worldnet
Guest





PostPosted: Mon Nov 07, 2005 9:34 am    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote


Francis Glassborow wrote:
Quote:
In article <436b5029$0$303$4d4ebb8e (AT) businessnews (DOT) de.uu.net>, Daniel
Krügler <dsp (AT) bdal (DOT) de> writes
The only real-world example for defining @= in terms of @
I'm aware of is operator overloading of enumeration types.


What is wrong with:

enum type { /* enumerator list */ };

type& operator +=(type & t, int i){
return t = type (t + i);
}

Note that the + is just a builtin operator+(int, int).

One problem I see with your example is that it does not account
for enum overflow. Any concept of enum arithmetic should be modular.

Jim Rogers


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
James Kanze
Guest





PostPosted: Mon Nov 07, 2005 10:45 am    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote

Mirek Fidler wrote:

Quote:
any reason to be worried about it: IMHO, any code which
defines and operator @ and does not define @= is already
broken, at least at the design level.

Like streams and operator<< ? Smile

Touché.

Ostream::<< has some pretty special semantics, which means that
even for a user defined class, I will define it. And no <<&,
obviously.

On the other hand, this is a very potent argument against the
Microsoft extension. I certainly don't what a mess of <<=
operators for ostream appearing out of nowhere.

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Daniel Krügler
Guest





PostPosted: Mon Nov 07, 2005 10:51 am    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote

Francis Glassborow wrote:
Quote:
In article <436b5029$0$303$4d4ebb8e (AT) businessnews (DOT) de.uu.net>, Daniel
Krügler <dsp (AT) bdal (DOT) de> writes

The only real-world example for defining @= in terms of @
I'm aware of is operator overloading of enumeration types.



What is wrong with:

enum type { /* enumerator list */ };

type& operator +=(type & t, int i){
return t = type (t + i);
}

Note that the + is just a builtin operator+(int, int).

Yes you could define operator+(type const &, int i) first if you want
but there is no need to.

While this is an alternative way to realize @= of an enumeration
type, its essentially **does** implement @= in terms of an @ operation,
although it is that of an integral type - I can't count that as a "pure"
@= implementation ;-)

By the way: In your example I would have expected something like

type& operator +=(type & t, type i) {
return t = type (t + int(i));
}

but I assume you wanted just to demonstrate the basic idea?

Greetings from Bremen,

Daniel

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Mon Nov 07, 2005 10:54 am    Post subject: Re: synthesized operators in C++ - a good thing? Reply with quote

Francis Glassborow wrote:
Quote:
In article <436b5029$0$303$4d4ebb8e (AT) businessnews (DOT) de.uu.net>,
Daniel Krügler <dsp (AT) bdal (DOT) de> writes

The only real-world example for defining @= in terms of @ I'm
aware of is operator overloading of enumeration types.

What is wrong with:

Nothing in itself but...

Quote:
enum type { /* enumerator list */ };

type& operator +=(type & t, int i){
return t = type (t + i);
}

Note that the + is just a builtin operator+(int, int).

Yes you could define operator+(type const &, int i) first if
you want but there is no need to.

All of the coding guidelines I have seen insist that if @= is
present, so is @ (and vice versa -- curiously, none of the
coding guidelines I've seen made an exception for the << and >>
of iostream, although it never occured to anyone to apply the
guideline to them). Code which defines +=, but not +, on a
type, is generally considered broken, or at least poorly
defined.

Given that, Daniel is right in that for enum types, it makes
more sense to define the | and & operators first, and |= and &=
in terms of them.

--
James Kanze GABI Software
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


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.