Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

allow lvalue change: cc and g++

7 views
Skip to first unread message

Venu Yanamandra

unread,
Nov 25, 2010, 8:35:55 AM11/25/10
to

I tried compiling the following program on g++ and it failed. The same
program failed with cc very rightly saying invalid lvalue.

---
testBox # cat check_lvalue_problem_c.c
#include <stdio.h>

main()
{
int i=5;

int k;
do
{
k = (--i)--;
printf("\n%d\n", (--i)-- );
}while(i>=2 && i < 5);
}
testBox # cc -o 12 check_lvalue_problem_c.c
check_lvalue_problem_c.c: In function āmainā:
check_lvalue_problem_c.c:10: error: invalid lvalue in decrement
check_lvalue_problem_c.c:11: error: invalid lvalue in decrement
testBox # g++ -o 12 check_lvalue_problem_c.c
testBox #
testBox # cat check_lvalue_problem.cpp
#include <iostream>

main()
{
int i=5;

int k;
do
{
k = (--i)--;
std::cout << (--i)-- << " ";
}while(i>=2 && i < 5);
}
testBox #
testBox # g++ -o 13 check_lvalue_problem.cpp
testBox #

---

could you please let me know what the problems is?


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

Francis Glassborow

unread,
Nov 27, 2010, 2:09:41 PM11/27/10
to
On 25/11/2010 13:35, Venu Yanamandra wrote:
>
> I tried compiling the following program on g++ and it failed. The same
> program failed with cc very rightly saying invalid lvalue.

I do not understand. Did those two sentences seem to be entirely consistent with
each other.
However note that the pre-increment operator for fundamental types in C++
evaluates to an lvalue (reference) see 5.3.2 para 1. IIRC that is a point of
difference between C and C++.


>
> ---
> testBox # cat check_lvalue_problem_c.c
> #include<stdio.h>
>
> main()
> {
> int i=5;
>
> int k;
> do
> {
> k = (--i)--;
> printf("\n%d\n", (--i)-- );

> }while(i>=2&& i< 5);


> }
> testBox # cc -o 12 check_lvalue_problem_c.c
> check_lvalue_problem_c.c: In function āmainā:
> check_lvalue_problem_c.c:10: error: invalid lvalue in decrement
> check_lvalue_problem_c.c:11: error: invalid lvalue in decrement
> testBox # g++ -o 12 check_lvalue_problem_c.c
> testBox #
> testBox # cat check_lvalue_problem.cpp
> #include<iostream>
>
> main()
> {
> int i=5;
>
> int k;
> do
> {
> k = (--i)--;
> std::cout<< (--i)--<< " ";

> }while(i>=2&& i< 5);


> }
> testBox #
> testBox # g++ -o 13 check_lvalue_problem.cpp
> testBox #
>
> ---
>
> could you please let me know what the problems is?
>
>

--
Note that robinton.demon.co.uk addresses are no longer valid.

Matt Calabrese

unread,
Nov 29, 2010, 2:20:41 PM11/29/10
to
On Nov 27, 2:09 pm, Francis Glassborow

<francis.glassbo...@btinternet.com> wrote:
> However note that the pre-increment operator for fundamental types in C++
> evaluates to an lvalue (reference) see 5.3.2 para 1. IIRC that is a point of
> difference between C and C++.

Also, even though (--i)-- where i is an int should compile in C++, it
technically has undefined behavior since you are reading and writing
to i multiple times between sequence points.

--

Francis Glassborow

unread,
Nov 29, 2010, 8:15:06 PM11/29/10
to
On 29/11/2010 19:20, Matt Calabrese wrote:
> On Nov 27, 2:09 pm, Francis Glassborow
> <francis.glassbo...@btinternet.com> wrote:
>> However note that the pre-increment operator for fundamental types in C++
>> evaluates to an lvalue (reference) see 5.3.2 para 1. IIRC that is a point of
>> difference between C and C++.
>
> Also, even though (--i)-- where i is an int should compile in C++, it
> technically has undefined behavior since you are reading and writing
> to i multiple times between sequence points.
>

Yes, that is a nasty one, though I would hope that most compilers would
at least raise a query (of course, what makes it worse is that it not
only compiles but will work until the moment that a venture capitalist
is looking at your work to decide whether to fund it :)

Venu Yanamandra

unread,
Nov 30, 2010, 5:27:11 AM11/30/10
to
On Nov 30, 12:20 am, Matt Calabrese <rivo...@gmail.com> wrote:
> On Nov 27, 2:09 pm, Francis Glassborow
>
> <francis.glassbo...@btinternet.com> wrote:
> > However note that the pre-increment operator for fundamental types in C++
> > evaluates to an lvalue (reference) see 5.3.2 para 1. IIRC that is a point of
> > difference between C and C++.
>
> Also, even though (--i)-- where i is an int should compile in C++, it
> technically has undefined behavior since you are reading and writing
> to i multiple times between sequence points.

{ banner removed. please do so yourself. -mod }

As I had included the brackets, I feel I am not messing with the
sequence points issue. Please correct me if this aint the case.

{ mod factoid: that "ain't the case". -mod }

Francis Glassborow

unread,
Nov 30, 2010, 9:51:19 PM11/30/10
to
On 30/11/2010 10:27, Venu Yanamandra wrote:
> On Nov 30, 12:20 am, Matt Calabrese<rivo...@gmail.com> wrote:
>> On Nov 27, 2:09 pm, Francis Glassborow
>>
>> <francis.glassbo...@btinternet.com> wrote:
>>> However note that the pre-increment operator for fundamental types in C++
>>> evaluates to an lvalue (reference) see 5.3.2 para 1. IIRC that is a point of
>>> difference between C and C++.
>>
>> Also, even though (--i)-- where i is an int should compile in C++, it
>> technically has undefined behavior since you are reading and writing
>> to i multiple times between sequence points.
>
> { banner removed. please do so yourself. -mod }
>
> As I had included the brackets, I feel I am not messing with the
> sequence points issue. Please correct me if this aint the case.
>
> { mod factoid: that "ain't the case". -mod }
>

An more extended:

Brackets change the order of evaluation of operators but have no effect
on sequence points. Nor do they have much influence on the order of
evaluation of sub expressions. E.g.

a = expr1 + expr2 * expr3;
b = (expr1 + expr2) * expr3;

In the first case the multiplication must be done before the addition
and vice versa in the second case. However the program is at liberty to
evaluate expr1, expr2 and expr3 in any order that it chooses. Indeed if
those two lines came consecutively there might be no need to evaluate
the expressions for the second case. However that depends on the
expressions in question.

It is a far too common misconception that parentheses have anything to
do with sequence points (I have known supposed experts get it wrong.
Indeed, for years the MISRA C site had it wrong :(

0 new messages