I was under the impression that it is better to use to use prefix
operators than to use postfix operators when one has a choice, because
the former doesn't require a temporary (In case of overloaded
operators, assuming they are written to mimic the usual meaning) . In
case of in-built types, the compiler can optimise away the temporary
(in case of the postfix operators), on detecting that the return value
is not used. Won't it be a better coding style to always use prefix,
even for in-built types, when there is a choice? However, in TC++PL,
postfix operators are used at times, even when the return value is to
ignored. Is there more to this than I see?
-- --
Abstraction is selective ignorance. -Andrew Koenig
-- --
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
kprateek88
|
11/4/2004 12:09:08 PM |
|
Prateek R Karandikar wrote:
> I was under the impression that it is better to use to use prefix
> operators than to use postfix operators when one has a choice, because
> the former doesn't require a temporary (In case of overloaded
> operators, assuming they are written to mimic the usual meaning) . In
> case of in-built types, the compiler can optimise away the temporary
> (in case of the postfix operators), on detecting that the return value
> is not used.
So, it doesn't really matter for built-ins, does it?
> Won't it be a better coding style to always use prefix,
> even for in-built types, when there is a choice?
For a novice it would be a good style, probably.
> However, in TC++PL,
> postfix operators are used at times, even when the return value is to
> ignored. Is there more to this than I see?
Surely there is more to this. Using pre-increment all the time is a good
practice for novices who need some good habits formed before they can
venture into the real world. Using post-increment and not pre-increment
where it really doesn't matter is a prerogative of a mature programmer.
OOH, an author of a book targeting _novices_ should perhaps follow the
"use pre-increment because it promotes forming of a good habit" rule.
OTOH, any author is free to do whatever if the end-result doesn't depend
on the method used.
After all, style is a personal preference. It can always be judged. And
we have waaaaay too many judges of style around.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Victor
|
11/5/2004 3:22:37 AM
|
|
> Is there more to this than I see?
Does the answer for "Which is more efficient: i++ or ++i?" contain
informations that you do not know already?
http://www.fmi.uni-konstanz.de/~kuehl/c++-faq/operator-overloading.html#faq-13.12
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Markus
|
11/5/2004 3:19:19 PM
|
|
"Prateek R Karandikar" <kprateek88@yahoo.com> wrote in message
news:607f883e.0411030206.492a9cda@posting.google.com...
> I was under the impression that it is better to use to use prefix
> operators than to use postfix operators when one has a choice, because
> the former doesn't require a temporary (In case of overloaded
> operators, assuming they are written to mimic the usual meaning) . In
> case of in-built types, the compiler can optimise away the temporary
> (in case of the postfix operators), on detecting that the return value
> is not used. Won't it be a better coding style to always use prefix,
> even for in-built types, when there is a choice? However, in TC++PL,
> postfix operators are used at times, even when the return value is to
> ignored. Is there more to this than I see?
C programmers often prefer postfix operators, maybe because of the
symmetry between: a+=2;
and: a++;
Old habits die hard...
The same could be said of the usage of p<end instead of p!=end
(e.g. as the exit condition of a for loop). In C++ the latter is
known to be more general, but you'll often see code using
the "classic" form, which some perceive as safer...
Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Ivan
|
11/5/2004 5:24:56 PM
|
|
You are correct that the prefix operators should be prefered to
postfix.
see: http://www.gotw.ca/gotw/074.htm, search for "Prefer using prefix"
I believe that the popularity of the postfix operators come from the
name of the language: "c++".
__
Jonas Norberg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Jonas
|
11/5/2004 5:26:28 PM
|
|
Prateek R Karandikar wrote:
> I was under the impression that it is better to use to use prefix
> operators than to use postfix operators when one has a choice, because
> the former doesn't require a temporary (In case of overloaded
> operators, assuming they are written to mimic the usual meaning) .
True, and I think it was a sad mistake in the language design to have
let the operator functions do everything on their own with such a big
freedom, which makes people have to "unlearn" i++ and change into ++i
even when they don't have to care whether it's a prefix or a postfix:
http://groups.google.com/groups?selm=bd47bb0e.0306280817.17db0fe2%40posting.google.com
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Seungbeom
|
11/6/2004 2:01:12 PM
|
|
"Ivan Vecerina" <INVALID_use_webform_instead@vecerina.com> wrote in
message news:<cme0vh$hm1$1@newshispeed.ch>...
> "Prateek R Karandikar" <kprateek88@yahoo.com> wrote in message
> news:607f883e.0411030206.492a9cda@posting.google.com...
> > I was under the impression that it is better to use to use prefix
> > operators than to use postfix operators when one has a choice,
> > because the former doesn't require a temporary (In case of
> > overloaded operators, assuming they are written to mimic the usual
> > meaning) . In case of in-built types, the compiler can optimise
> > away the temporary (in case of the postfix operators), on detecting
> > that the return value is not used. Won't it be a better coding
> > style to always use prefix, even for in-built types, when there is
> > a choice? However, in TC++PL, postfix operators are used at times,
> > even when the return value is to ignored. Is there more to this
> > than I see?
> C programmers often prefer postfix operators, maybe because of the
> symmetry between: a+=2;
> and: a++;
> Old habits die hard...
That's the first time I've ever been appraised of the symmetry. I know
that I prefer a++ for the simple reason that that is what Kernighan and
Richie did. As to why Kernighan and Richie chose this style, I can only
guess: they were working mainly on a PDP-11, and the machine
instructions have post-increment, or perhaps most of the time they did
use the value, they needed post-increment, so it seemed more likely. Or
maybe thy just flipped a coin, since it didn't make a difference.
> The same could be said of the usage of p<end instead of p!=end
> (e.g. as the exit condition of a for loop). In C++ the latter is known
> to be more general, but you'll often see code using the "classic"
> form, which some perceive as safer...
In general, I would say use < for integral indexes, and != for anything
pointer-like. The < expresses part of the loop invariant more
precisely.
--
James Kanze GABI Software http://www.gabi-soft.fr
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! ]
|
|
0
|
|
|
|
Reply
|
kanze
|
11/8/2004 11:36:19 PM
|
|
"Seungbeom Kim" <musiphil@bawi.org> wrote in message
news:cmi6fh$erq$1@news.Stanford.EDU...
> Prateek R Karandikar wrote:
>
> > I was under the impression that it is better to use to use prefix
> > operators than to use postfix operators when one has a choice, because
> > the former doesn't require a temporary (In case of overloaded
> > operators, assuming they are written to mimic the usual meaning) .
>
> True, and I think it was a sad mistake in the language design to have
> let the operator functions do everything on their own with such a big
> freedom, which makes people have to "unlearn" i++ and change into ++i
> even when they don't have to care whether it's a prefix or a postfix:
>
> http://groups.google.com/groups?selm=bd47bb0e.0306280817.17db0fe2%40posting.google.com
I don't see how programmer conventions and misconceptiosn can be considered
a mistake of the language design. I agree certain implementations of
operators are less than ideal, but that is a programmer issue not a language
issue. It is best though that the language design be flexible, I would argue
in fact that it was in fact a mistake to predefine all of the primitive
operations. In Heron, for instance, all primitive operations are defined in
the standard library so the operators require great freedom.
Christopher Diggins
http://www.heron-language.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
christopher
|
11/12/2004 12:07:22 PM
|
|
|
7 Replies
154 Views
(page loaded in 0.115 seconds)
Similiar Articles: naming convention for class attributes (member data)? - comp.lang ...Isn't that adding to the confusion, rather than reducing it? ... is causing headaches and so is better ... "z_" or "Z_" usually a "don't use" prefix (means ... enum and operator++ - comp.lang.c++I have an enum and a prefix ++ operator: enum Dir ... Why ++d isn't enough and we have to use d =3D ++d? ... Incrementing an enum variable? - comp.lang.c Enum Postfix ... Tried F1, Google, my var etc..... :-P - comp.cad.solidworks ...I get the feeling this isn't what you're asking, but if ... in the old Drawing templates > (with $PRPSHEET: prefix ... Ahh . . . that's better. OK, there are a number of ways ... Fast bit-reverse on an x86? - comp.dsp... it's not real time, but real fast is still better than ... unknown-linux-gnu --target=x86_64-pc-mingw32 --prefix ... > > You know that just isn't right. =A0In this case the ... xchg & lock question - comp.lang.asm.x86... the cmpxchg loop will work much better on a uniprocessor system since without the lock prefix the cmpxchg loop *won't* do the ... memory (for example on a I/O card) isn't a ... improve strlen - comp.lang.asm.x86C compilers aren't better than assembly programmers, they ... value, before returning from ++ operator (postfix) we ... course), but I do know that claims of "this isn't better ... mailx without sendmail? - comp.unix.solarisAFAIK, mailx isn't one of them. -- DeeDee, don't press that button! DeeDee! ... but no running mta agent would be better. i run sendmail because it's updated via ... Limiting max swap for a group of processes - comp.unix.solaris ...... related to the amount of free memory, but isn't exactly ... Build: % gmake % gmake install PREFIX=/opt/slayerd ... normally turn it off, so I don't know if it's any better ... input & output in assembly - comp.lang.asm.x86Wouldn't it be much better to use ... binary to less than one or >two Kibibytes. It isn't ... at no extra > (prefix) cost! > from a memory access point of view, though... isn't ... NFS performance tuning help - comp.unix.solarisI'm getting less than 16% of peak performance. I'm at ... on the client ../configure --enable-burst --prefix ... This isn't really a cheat, I just wanted to throw this ... Infix to PostFix and Prefix (The Begining...) - GIDForumsInfix to PostFix and Prefix (The Begining...) ... So: If it isn't covered in your text or class ... for some godawful reason) would be better for teaching than ... c++ - postfix and prefix increment operator in a for loop - Stack ...If col was a class, the prefix and postfix '++' operators might be ... can lead to overflow related concerns whereas << 1 won't. So isn't (num_to_fill << 1) better than ... 7/22/2012 1:30:07 AM
|