如何获得if表达式的哪一部分为真?

Mah*_*jan 1 c c++ if-statement objective-c

假设我有以下代码:

if(condition1 || condition2 || condition 3 || condition4)
{
// this inner part will be executed if one of the conditions is true.
// Now I want to know by which condition this part is executed.
}
Run Code Online (Sandbox Code Playgroud)

jro*_*rok 5

我确信有更好的方法可以做到这一点,这里有一个:

int i = 0;
auto check = [&i](bool b)->bool
{
    if (!b) ++i;
    return b;
};

if (check(false) || // 0
    check(false) || // 1
    check(true)  || // 2
    check(false))   // 3
{
    std::cout << i; // prints 2
}
Run Code Online (Sandbox Code Playgroud)