May one write something like this in c :
// ...
if(value <= switch(secValue){
case First:
return 1;
case Second:
return 2;
return -1;
}){
//... do some logic ...
} // end if
Thanks
CodePudding user response:
Exactly this, no. But you could get close with a lambda expression.
Example:
#include <iostream>
int main()
{
// dummy types and values needed for demo
enum test_enum
{
First, Second
} secValue = First;
int value = 1;
if (value <= [secValue]()
// ^ Capture secValue in lambda expression
{
switch (secValue)
{
case First:
return 1;
case Second:
return 2;
default: // needed default case. Can't just have a stray return
// in a switch
return -1;
}
}())
// ^ immediately invoke lambda
{
std::cout << "Huzah!";
}
}
CodePudding user response:
Not with a `switch’, but with the ternary operator:
if (value <= (secValue == First ? 1
: secValue == Second ? 2
: -1)) {
// . . .
}
But you really should put the right-hand side in a function so that the code is more readable.