3 ms·
I use something similar in a container library to warn the user if he or she supplies an argument with potential side effects to a macro that evaluates it multi
by JacksonAllan 1y ago
I use something similar in a container library to warn the user if he or she supplies an argument with potential side effects to a macro that evaluates it multiple times:
https://github.com/JacksonAllan/CC/blob/42a7d810274a698dff87a6cdbecd91e93798afad/cc.h#L1284 https://github.com/JacksonAllan/CC/blob/42a7d810274a698dff87...
Specifically, if (arg)==(arg) is not a constant expression, then it could have side effects.
However, this mechanism does generate some annoying false positives, as shown below:
// Create a map with int keys and values that are vectors of floats:
map( int, vec( float ) ) our_map;
init( &our_map );
// Create a vector of floats:
vec( float ) our_vec;
init( &our_vec );
push( &our_vec, 1.23f );
// Insert the vector into the map.
insert( &our_map, 456, our_vec );
// Generates a warning because get checks its first argument for side
// effects and the compiler can't tell that the first argument of the
// outermost get has none:
printf( "%f", *get( get( &our_map, 456 ), 0 ) );
// The "proper", albeit cumbersome, way to achieve the same thing without a
// warning:
vec( float ) *itr = get( &our_map, 456 );
printf( "%f", *get( itr, 0 ) );