Essentially the same as this question, but for C++.
The simplest way to do this would be:
if (condition) {
a = f(x);
} else {
b = f(x);
}
While this meets my needs, it has always been bugging me that I've had to repeat typing f(x) twice, not to mention expanding the code out over so many lines when only one line will be executed. What if I have a larger set of destination variables to choose from?
switch(condition variable) {
case 1:
var1 = f(x);
break;
case 2:
var2 = f(x);
break;
...
case y:
vary = f(x);
break;
}
This looks very inelegant to me.
Is there anything in C++ that essentially allows me to do the following?
do-something-that-returns-a-lvalue = f(x);
Pardon me if it's not called a lvalue, I'm still relatively new to C++. But you guys know what I mean - the destination variable to assign a value to.