R
Rui Maciel
Did anyone ever used the comma operator in some place other than a loop
condition?
Thanks in advance,
Rui Maciel
condition?
Thanks in advance,
Rui Maciel
Did anyone ever used the comma operator in some place other than a loop
condition?
Did anyone ever used the comma operator in some place other than a loop
condition?
Did anyone ever used the comma operator in some place other than a loop
condition?
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.
With C++11's initializer lists this trick might go the way of the dodo, but
it is still a neat trick, though.
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);
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);
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
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:
Did anyone ever used the comma operator in some place other than a loop
condition?
Did anyone ever used the comma operator in some place other than a loop
condition?
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().
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"...
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.
Victor said:How would it be equivalent?
No, they don't. 'static_assert' has behavior in the *compile-time*
where as 'assert' exhibits its in the *run-time*.
That does not make sense, sorry.
Did anyone ever used the comma operator in some place other than a loop
condition?
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)
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.