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 

Strange C++ construct

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Trevor Morgan
Guest





PostPosted: Wed Feb 08, 2006 3:00 pm    Post subject: Strange C++ construct Reply with quote



Can anyone help we with this construct? I thought I knew the language
pretty well, but I'm really not sure what this is doing.

This code:

int i = (1,2,3);

compiles fine, and sets i to 3. What is the exact meaning of this
syntax? Where is it described in the standard or in Stroustrup?

Thanks in advance.


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





PostPosted: Wed Feb 08, 2006 5:00 pm    Post subject: Re: Strange C++ construct Reply with quote



Trevor Morgan wrote:
Quote:
Can anyone help we with this construct? I thought I knew the language
pretty well, but I'm really not sure what this is doing.

This code:

int i = (1,2,3);

compiles fine, and sets i to 3. What is the exact meaning of this
syntax? Where is it described in the standard or in Stroustrup?

It's the comma operator (not to be confused with the , function

parameter separator, or the , list separator) - who's effect is to:
a) evaluate the thing on the left hand side & throw the result away
b) evaluate the thing on the right hand side and return that as the
result of the operation.

The brackets ensure that the , is taken as a comma operator, rather
than a list separator.

So int i = (1, 2, 3) says
evaluate 1 & discard
evaluate 2 & discard
set i to 3.

In c++, that's a singularly ropy way of doing things, you could just do
1;
2;
int i = 3;

it looks as though that code might have been ported from c, where it
makes a lot more sense.

The , operator is useful for avoiding the dreaded assignment in if
statement syntax:

instead of if (a = complexfunction(b))

you can write if (a = complexfunction(b), a != 0)

which is clearer (well, I think it is), and it doesn't precipitate
compiler warnings


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





PostPosted: Wed Feb 08, 2006 5:00 pm    Post subject: Re: Strange C++ construct Reply with quote



In article <1139401358.826611.264500 (AT) g44g2000cwa (DOT) googlegroups.com>,
"Trevor Morgan" <trevor_morgan (AT) yahoo (DOT) com> wrote:

Quote:
Can anyone help we with this construct? I thought I knew the language
pretty well, but I'm really not sure what this is doing.

This code:

int i = (1,2,3);

compiles fine, and sets i to 3. What is the exact meaning of this
syntax? Where is it described in the standard or in Stroustrup?

Thanks in advance.

Stroustrup, "The C++ Programming Language" section 6.2.2.

The comma operator evaluates the left hand argument then evaluates the
right hand argument and returns it. So for example:

int a, b;
b = ( a = 2, a + 1 );
assert( b == 3 );

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.

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





PostPosted: Wed Feb 08, 2006 5:00 pm    Post subject: Re: Strange C++ construct Reply with quote

In message <1139401358.826611.264500 (AT) g44g2000cwa (DOT) googlegroups.com>,
Trevor Morgan <trevor_morgan (AT) yahoo (DOT) com> writes
....
Quote:

This code:

int i = (1,2,3);

compiles fine, and sets i to 3. What is the exact meaning of this
syntax? Where is it described in the standard or in Stroustrup?

Its the comma operator. On the RHS it evaluates the expression L to R,

and the whole expression takes the value of the rightmost term. Sorry,
no refs to hand, but I'm sure you can look up the operator. Wink
--
Alec Ross

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





PostPosted: Wed Feb 08, 2006 5:00 pm    Post subject: Re: Strange C++ construct Reply with quote

"Trevor Morgan" <trevor_morgan (AT) yahoo (DOT) com> wrote in message
news:1139401358.826611.264500 (AT) g44g2000cwa (DOT) googlegroups.com...
: Can anyone help we with this construct? I thought I knew the language
: pretty well, but I'm really not sure what this is doing.
:
: This code:
:
: int i = (1,2,3);
:
: compiles fine, and sets i to 3. What is the exact meaning of this
: syntax? Where is it described in the standard or in Stroustrup?

Look for the "comma operator".
When used outside of contexts where it is given a special meaning
as an argument or value separator, the comma is treated as an
operator that evaluates its operands in left-to-right order,
and returns the rightmost value.

Here's the relevant extract from the C++ standard:

<<<<
5.18 Comma operator [expr.comma]

<1>
The comma operator groups left-to-right.
expression:
assignment-expression
expression , assignment-expression
A pair of expressions separated by a comma is evaluated left-to-right and
the value of the left expression is discarded. The lvalue-to-rvalue
(4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard
conversions are not applied to the left expression. All side effects
(1.9) of the left expression, except for the destruction of temporaries
(12.2), are performed before the evaluation of the right expression. The
type and value of the result are the type and value of the right operand;
the result is an lvalue if its right operand is.

<2>
In contexts where comma is given a special meaning, [Example: in lists of
arguments to functions (5.2.2) and lists of initializers (8.5) ] the comma
operator as described in clause 5 can appear only in parentheses.
[Example:
f(a, (t=3, t+2), c);
has three arguments, the second of which has the value 5. ]
Quote:


hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com



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





PostPosted: Wed Feb 08, 2006 7:10 pm    Post subject: Re: Strange C++ construct Reply with quote

Trevor Morgan wrote:
Quote:
Can anyone help we with this construct? I thought I knew the language
pretty well, but I'm really not sure what this is doing.

This code:

int i = (1,2,3);

compiles fine, and sets i to 3. What is the exact meaning of this
syntax? Where is it described in the standard or in Stroustrup?

Read about "the comma operator". It's been around for ages. "TC++PL",
Special Edition, has it on page 123. The Standard has it in the subclause
5.18.

V
--
Please remove capital As from my address when replying by mail

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





PostPosted: Thu Feb 09, 2006 11:06 am    Post subject: Re: Strange C++ construct Reply with quote

Thanks all for your help. Now I remember that I have seen this before
- we had a long debate once in a code-review meeting as to whether this
was legal, and had to go back to Stroustrup to convince ourselves. I'd
forgotten all about it - I was assuming that it was something to do
with brackets, not commas.

Of course, in C++ you can even overload the comma operator, but I can't
imagine a circumstance when you'd want to do that, apart from possibly
creating yourself a bit of job-security....


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





PostPosted: Fri Feb 10, 2006 11:06 am    Post subject: Re: Strange C++ construct Reply with quote

In article <1139476878.601967.58460 (AT) g44g2000cwa (DOT) googlegroups.com>,
Trevor Morgan <trevor_morgan (AT) yahoo (DOT) com> wrote:

Quote:
Of course, in C++ you can even overload the comma operator, but I can't
imagine a circumstance when you'd want to do that, apart from possibly
creating yourself a bit of job-security....


Or writing a mini language within C++ code such as phoenix. the

comma operator could combine the two expression templates its passed
into one that executes both expressions in order when it is executed at
runtime. such as print to count a bar graph of '.' of a vector<int>.
using namespace phoenix;
using namespace std;
//...
int iii;
for_each(data.begin(),data.end(),
if_(arg1 > 0)
[
for_(var(iii) = 0;var(iii)!=arg1;++var(iii))
[
cout << val('.') // no semicolons in the []'s here
], // note comma here...
cout << val('\n')
]
);

this equivalent to [produces the same results as]
using namespace std;
struct foo
{
void operator () (int x)
{
if(x > 0)
{
for(int i=0;i!=x;++i) cout << '.';
cout << '\n';
}
};
// ...
for_each(data.begin(),data.end(),foo());

as an example. Its not job security but usage of an available library
to put the loop code in the loop statement. For this example its
'overkill' but in the context of a real application it can produce
100's of implementation created functors for simple things, enhancing
STL algorithms by place small code segments directly in the algorithm
call.

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





PostPosted: Sat Feb 11, 2006 12:06 pm    Post subject: Re: Strange C++ construct Reply with quote

Trevor Morgan wrote:
Quote:
Of course, in C++ you can even overload the comma operator, but I can't
imagine a circumstance when you'd want to do that, apart from possibly
creating yourself a bit of job-security....



Did you see Boost parameter library which overloads comma operator and
produces some interesting usage?

http://www.boost.org/libs/parameter/doc/html/index.html


[ 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
Page 1 of 1

 
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.