Any use for the comma operator?

T

Tobias Müller

Rui Maciel said:
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.



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.



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


Rui Maciel

The important property of static_assert is, that it triggers at compile
time, the additional message is just nice to have.
So essentially they have a very different _effect_, they just _look_
similar.

Tobi
 
C

Cholo Lennon

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.

A real usage can be found in Blitz++
(http://sourceforge.net/projects/blitz/) which uses the comma operator
to initialize arrays:

#include <blitz/array.h>

using namespace blitz;

int main()
{
Array<float,2> A(3,3), B(3,3), C(3,3);

A = 1, 0, 0,
2, 2, 2,
1, 0, 0;

B = 0, 0, 7,
0, 8, 0,
9, 9, 9;

C = A + B;

...
}


Regards
 
I

Ike Naar

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.

consider these two programs:

/* program A */
int main()
{
if (0)
{
assert("message", false);
}
return 0;
}

/* program B */
int main()
{
if (0)
{
static_assert(false, "message");
}
return 0;
}

Program A will compile and run successfully (without the assertion
being triggered); program B will not compile.
 
I

Ike Naar

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.

consider these two programs:

/* program A */
int main()
{
if (0)
{
assert(("message", false));
}
return 0;
}

/* program B */
int main()
{
if (0)
{
static_assert(false, "message");
}
return 0;
}

Program A will compile and run successfully (without the assertion
being triggered); program B will not compile.
 
R

Rui Maciel

Tobias said:
The important property of static_assert is, that it triggers at compile
time, the additional message is just nice to have.
So essentially they have a very different _effect_, they just _look_
similar.

The effect I referred to was the ability to assert if a condition had been
met. They obviously don't work the same way with regards to other aspects.
This was one of the reasons the comma operator/assert() trick was described
as a "poor man's version" of static_assert() instead of being an exact
match.


Rui Maciel
 
Ö

Öö Tiib

The effect I referred to was the ability to assert if a condition had been
met. They obviously don't work the same way with regards to other aspects.
This was one of the reasons the comma operator/assert() trick was described
as a "poor man's version" of static_assert() instead of being an exact
match.

That brings mental image of a poor man who can't afford downloading a header that implements BOOST_STATIC_ASSERT() for him.

That poor man needs (to get static_assert effect out of assert) to write unit test (a tiny program) that asserts same things as static_asserts assert in code. Then he has always to compile and run that test prior to compilinghis code. I am not sure how comma operator helps him.
 
N

Noah Roberts

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

condition?

Here's a horrible way to use it:

struct funky_int {
int value();
funky_int(int v);
};

struct matrix {
int operator[](pair<funky_int,funky_int>);
};

pair<funky_int,funky_int> operator , (funky_int, funky_int);

matrix m;
m[funky_int(5),funky_int(7)];
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top