Friday, 6 September 2013

Blank or do-nothing 2nd or 3rd argument for ternary operator

Blank or do-nothing 2nd or 3rd argument for ternary operator

Is it possible to have a blank 2nd or 3rd argument for the ternary
operator or have one that is non-context-specific and means "do nothing"?
In the following example, I want a ternary operator to multiply an integer
variable by two if it is even or do nothing otherwise. I cannot think of
anything for the third argument other than self-assignment, adding or
subtracting zero, or multiplication or division by one. Anyway, they are
all context-specific. I want something that means "do nothing" for all
ternary operators. I tried leaving the argument blank, but it wouldn't
compile.
#include <iostream>
int main()
{
int x = 5;
(x % 2 == 0) ? x *= 2 : x = x; // or x += 0, x -= 0, x *= 1, or x /= 1
std::cout << x << std::endl;
return 0;
}

No comments:

Post a Comment