Any use for the comma operator?

R

Rui Maciel

Did anyone ever used the comma operator in some place other than a loop
condition?


Thanks in advance,
Rui Maciel
 
V

Victor Bazarov

Did anyone ever used the comma operator in some place other than a loop
condition?

Absolutely. If you can find a reference on the 'net to "list assignment
trick" or "technique", you can implement your own list class in such a
way that would allow writing something like

MyList<int> mylist;
...
mylist = 1, 2, 4;

for instance. I honestly have no opinion on convenience of such code.
But it looks like fun.

V
 
K

Kaba

16.8.2012 19:20, Rui Maciel kirjoitti:
Did anyone ever used the comma operator in some place other than a loop
condition?

Consider a class A which is a container of some kind. Then with suitable
definitions it could be filled with the syntax

A a;
a += 1, 2, 3, 4;

I did that for my matrix class. However, with C++11 and initializer
lists such uses should decline.
 
L

Luca Risolia

Did anyone ever used the comma operator in some place other than a loop
condition?

Sometimes I used it to dynamically initialize some static data before
main(), for example:

// .h
struct S {
static void compute() { // non-const expr
// ...
}
static int x[], y[];
static bool init;
};

// .cpp
int S::x[100], y[100];
bool S::init = (S::compute(), true);
 
R

Rui Maciel

Victor said:
Absolutely. If you can find a reference on the 'net to "list assignment
trick" or "technique", you can implement your own list class in such a
way that would allow writing something like

MyList<int> mylist;
...
mylist = 1, 2, 4;

for instance. I honestly have no opinion on convenience of such code.
But it looks like fun.

Thanks for mentioning that trick. Meanwhile, while googling for "list
assignment trick" I've stumbled on Boost's assignment library, which might
be of some interest:

http://www.boost.org/doc/libs/1_47_0/libs/assign/doc/index.html


With C++11's initializer lists this trick might go the way of the dodo, but
it is still a neat trick, though.


Rui Maciel
 
K

Kaba

16.8.2012 22:21, Rui Maciel kirjoitti:
With C++11's initializer lists this trick might go the way of the dodo, but
it is still a neat trick, though.

Well, yes:) More generally, though, such tricks reflect a language's
weakness in its abstraction capability. Fortunately that's fixed. Let's
hope to get polymorphic lambdas and concepts in the next iteration:)
 
R

Rui Maciel

Luca said:
Sometimes I used it to dynamically initialize some static data before
main(), for example:

// .h
struct S {
static void compute() { // non-const expr
// ...
}
static int x[], y[];
static bool init;
};

// .cpp
int S::x[100], y[100];
bool S::init = (S::compute(), true);

Nice one. It also looks like a nifty way to add some semantics to an
assignment.


Rui Maciel
 
V

Victor Bazarov

Did anyone ever used the comma operator in some place other than a loop
condition?

Sometimes I used it to dynamically initialize some static data before
main(), for example:

// .h
struct S {
static void compute() { // non-const expr
// ...
}
static int x[], y[];
static bool init;
};

// .cpp
int S::x[100], y[100];
bool S::init = (S::compute(), true);

Seems that if you just make your 'compute' return a bool, then there is
no need in the comma:

bool S::init = S::compute();

V
 
8

88888 Dihedral

Scott Lurndalæ–¼ 2012å¹´8月17日星期五UTC+8上åˆ1時10分50秒寫é“:
Bjarne Stroustrup used it extensively in the C-code generated by cfront.



http://en.wikipedia.org/wiki/Cfront



Very extensively, to the point that it exposed various bugs in PCC on certain

architectures. In a box somewhere, I still have a diagram of an expression

tree generated by PCC when compiling cfront-generated code. PCC would run out

of temporary registers (on a motorola 88100) trying to generate code for the

expression (I used sethi-ullman numbers to fix this).



http://en.wikipedia.org/wiki/Portable_C_Compiler



I still use it to group related items.



example:



len = snprintf(bp, remaining, "fmt", ...);

bp += len, remaining -= len;



scott

I'll vote for the old style main(int argc, void** argv) for
varried number of arguments.

But the callee has to know the rules to get the
varied number of terms from the caller.
 
L

Luca Risolia

Did anyone ever used the comma operator in some place other than a loop
condition?

Sometimes I used it to dynamically initialize some static data before
main(), for example:

// .h
struct S {
static void compute() { // non-const expr
// ...
}
static int x[], y[];
static bool init;
};

// .cpp
int S::x[100], y[100];
bool S::init = (S::compute(), true);

Seems that if you just make your 'compute' return a bool, then there is
no need in the comma:

Of course, if you can and want to change the return type.
 
S

see.my.homepage

W dniu czwartek, 16 sierpnia 2012 18:20:14 UTC+2 użytkownik Rui Macielnapisał:
Did anyone ever used the comma operator in some place other than a loop
condition?

You might want to check SOCI, the database access library for C++:

http://soci.sourceforge.net/

This library uses the comma operator to achieve very readable, concise syntax of database interactions.
 
N

Nick Keighley

Did anyone ever used the comma operator in some place other than a loop
condition?


slightly more helpful asserts, say in a switch

default:
assert (("unknown type of thingy", false));

though perhaps it would be better to make the string
"known type of thingy"...
 
R

Rui Maciel

Nick said:
slightly more helpful asserts, say in a switch

default:
assert (("unknown type of thingy", false));

though perhaps it would be better to make the string
"known type of thingy"...

Nice, a poor man's version of C++11's static_assert().


Rui Maciel
 
J

Jeff Flinn

Nice, a poor man's version of C++11's static_assert().

In what way? static_assert is a compile time assert, whereas the code
above is run time assert. Or am I missing something.

Jeff
 
V

Victor Bazarov

slightly more helpful asserts, say in a switch

default:
assert (("unknown type of thingy", false));

"type"? More like "value", perhaps. :)
though perhaps it would be better to make the string
"known type of thingy"...

We/I usually use

assert(<whatever_condition> || !"<reason for failing here>");

(no need for the comma or extra parens). So, in your case you'd do

default:
assert(!"unknown type of thingy");

right?

V
 
R

Rui Maciel

Jeff said:
In what way? static_assert is a compile time assert, whereas the code
above is run time assert. Or am I missing something.

That is true, static_assert() is a compile time assert while the assert()
macro performs the tests at run time. I was refering to the fact that, as
the expression passed to assert() is a constant expression, it is always
evaluated to:

assert(false);


Hence, in this case, as we are dealing with a constant condition, that
particular assert() would be equivalent to:

static_assert(false,"unknown type of thingy");


Then, in practical terms, both can present essentially the same behavior.
There would be important differences on how each one would perform,
particularly how the compile time version would be triggered when compiling
while the run time assertion could stay dormant in the code and only be
triggered if a thread of execution happens to reach a particular statement.
Yet, as both assertions can be employed to get essentially the same effect,
hence the "poor man's version" comment.


Rui Maciel
 
V

Victor Bazarov

That is true, static_assert() is a compile time assert while the assert()
macro performs the tests at run time. I was refering to the fact that, as
the expression passed to assert() is a constant expression, it is always
evaluated to:

assert(false);


Hence, in this case, as we are dealing with a constant condition, that
particular assert() would be equivalent to:

static_assert(false,"unknown type of thingy");

How would it be equivalent? If the compiler encounters *this*
static_assert, it will stop the compilation. The expression (it's
actually a macro, I believe) 'assert(false)', is OK, and will be
compiled and no diagnostic is required.
Then, in practical terms, both can present essentially the same behavior.

No, they don't. 'static_assert' has behavior in the *compile-time*
where as 'assert' exhibits its in the *run-time*.
There would be important differences on how each one would perform,
particularly how the compile time version would be triggered when compiling
while the run time assertion could stay dormant in the code and only be
triggered if a thread of execution happens to reach a particular statement.
Yet, as both assertions can be employed to get essentially the same effect,
hence the "poor man's version" comment.

That does not make sense, sorry.

V
 
R

Rui Maciel

Victor said:
How would it be equivalent?

Equivalent means having the same effect, and assertions are used to assert
that a specific condition is met. Knowing this, if you wish to assert that
a given condition is met, and that condition is expressed through a constant
condition, then, whether you employ

assert( ("message",condition))

....or

static_assert(condition, "message)


....you still get to determine if a condition has been met. That is, you
still get the same effect from either option. Hence, in the case under
consideration, they are equivalent.

No, they don't. 'static_assert' has behavior in the *compile-time*
where as 'assert' exhibits its in the *run-time*.

I'm sure you are aware that, in this context, you are nit-picking. I'm sure
you are quite able to come up with a scenario where both would be
equivalent, that is, both would have the same effect.

That does not make sense, sorry.

If you read it I'm sure you will understand.


Rui Maciel
 
A

army1987

Did anyone ever used the comma operator in some place other than a loop
condition?

I've used it a couple times (in C, not C++, though) in preprocessor
macros. (I think it's more elegant than the do { /* stuff */ } while(0)
kludge, and doesn't break should I use it in a loop condition later.)
 
L

Luca Risolia

Equivalent means having the same effect, and assertions are used to assert
that a specific condition is met. Knowing this, if you wish to assert that
a given condition is met, and that condition is expressed through a constant
condition, then, whether you employ

assert( ("message",condition))

...or

static_assert(condition, "message)

Sometimes you may want to run the dynamic assertion once.
So another possible use of comma is the following:

static bool run_assertion_once = (assert(something), true);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top