Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Legality of a++ = 1 ?

Reply
Thread Tools

Re: Legality of a++ = 1 ?

 
 
BartC
Guest
Posts: n/a
 
      03-09-2012
"Russell Shaw" <rjshawN_o@s_pam.netspace.net.au> wrote in message
news:(E-Mail Removed)...
>>>>> [ Legality of a++ = 1 ]


> I am writing a C compiler and it can be hard to pinpoint answers in the
> C99 standard. 6.5.2, 6.5.3, and its footnotes looks like a relevent answer
> to disallowing a reliable sequence of operations for a++ = b.


Then it depends on whether A++ can be an lvalue. The 'A' part will be
(otherwise you wouldn't be able to use ++ on it). But it's lvalue-ness is
already expended on '++'; Can you re-use this value on the result of the
whole 'A++' term?

If you can, then you will also be able to do &(A++), and A++ ++ ++, and all
sorts of weird stuff. But some of this, including A++=B would be changing a
variable twice between sequence points (which is a big no-no in C).

Apart from which, no-one looking at A++=B would have a clue what it's
supposed to mean. So if mainstream compilers won't accept it, then I would
just forget about it.

> Half the effort is in figuring out whether something that is technically
> doable is politically allowed by the standard.


It is very tempting when writing compilers to improve on the language,
especially it is trivial to do so. But any sorts of extensions to C are
usually frowned upon, unless ratified by a committee. Or you can just
forget the Standard...

--
Bartc

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-09-2012
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
<snip>
> I am writing a C compiler and it can be hard to pinpoint answers in
> the C99 standard. 6.5.2, 6.5.3, and its footnotes looks like a
> relevent answer to disallowing a reliable sequence of operations for
> a++ = b.


6.5.16.1 (simple assignment) is also important, of course. But C
disallows very little. The often feared "undefined behaviour" just
means that the C standard has nothing to say on the matter, so you are
free to do what you like in undefined situations.

a++ = b is a constraint violation so a diagnostic is required, but it
does not even have to relate to that piece of code. Your compiler could
announce at the start:

"This compiler accepts extensions to the C language."

and you will have conformed to the letter of the standard. Of course,
people like to be told where their code uses extensions, so something
more specific is helpful, despite not being required.

In this case, though, I have to wonder what the advantage of allowing
a++ = b is. It's not expensive to check since lvalue-ness (if I you can
forgive such an ugly term) is a property of syntactic forms. You can
flag up things like this with very little extra code at all.

> Half the effort is in figuring out whether something that is
> technically doable is politically allowed by the standard.


--
Ben.
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      03-09-2012


"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Harald van Dijk <(E-Mail Removed)> writes:
>> [ Legality of a++ = 1 ]


>> No, it's always a value. If you can come up with a concrete sane
>> proposal for making a++ an lvalue and a description of what the
>> semantics should be, preferably without breaking existing valid code,
>> I will be very impressed.

>
> For what it's worth, in C++ ++a is an lvalue (but a++ isn't).


How would the ++a work? Like this:

int a=1000;

++a += 3; // a is now 1004?

--
Bartc

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-09-2012
"BartC" <(E-Mail Removed)> writes:
> "Keith Thompson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Harald van Dijk <(E-Mail Removed)> writes:
>>> [ Legality of a++ = 1 ]

>
>>> No, it's always a value. If you can come up with a concrete sane
>>> proposal for making a++ an lvalue and a description of what the
>>> semantics should be, preferably without breaking existing valid code,
>>> I will be very impressed.

>>
>> For what it's worth, in C++ ++a is an lvalue (but a++ isn't).

>
> How would the ++a work? Like this:
>
> int a=1000;
>
> ++a += 3; // a is now 1004?


I get 1004 using the g++ compiler -- but as in C, modifying an object
twice without an intervening sequence point has undefined behavior.

I think the usefulness of allowing ++a to be an lvalue shows up when
"++" is an overloaded operator.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      03-09-2012
On Mar 9, 8:17*pm, Keith Thompson <(E-Mail Removed)> wrote:
> "BartC" <(E-Mail Removed)> writes:
> > "Keith Thompson" <(E-Mail Removed)> wrote in message
> >> For what it's worth, in C++ ++a is an lvalue (but a++ isn't).

>
> > How would the ++a work? Like this:

>
> > int a=1000;

>
> > ++a += 3; * *// a is now 1004?


Yes.

> I get 1004 using the g++ compiler -- but as in C, modifying an object
> twice without an intervening sequence point has undefined behavior.


C++ introduces sequence points after the side effect for prefix ++.
(Actually, it does so for a += 1, and ++a is defined to be equivalent
to a += 1.) It has to, or the lvalue-to-rvalue conversion in a = ++b
would be undefined. The example of a ^= b ^= a ^= b, given in the C
FAQ as an example of undefined behaviour, is valid in C++.

> I think the usefulness of allowing ++a to be an lvalue shows up when
> "++" is an overloaded operator.


When ++ is an overloaded operator, the overloaded operator determines
the type and lvalue-ness of the result. That's no reason why the built-
in types' prefix ++ should return lvalues.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wireless network legality bassetrox@moose-mail.com Wireless Networking 5 04-16-2006 03:49 AM
Legality of type conversion on instance ports? Brandon VHDL 1 08-04-2005 04:57 AM
Action Pack - legality? BWGames Microsoft Certification 8 12-02-2004 07:37 PM
Complicated music downloading legality question vbMark Computer Support 14 11-12-2004 06:40 AM
Legality of selling my MCSD CTEC Course Folders Raj Singh MCSD 4 01-19-2004 06:23 PM



Advertisments