简体   繁体   English

操作的条件运算符?

[英]conditional operator for operations?

there is the conditional operator x?a:b , which often saves a lot of writing 有条件运算符x?a:b ,它通常可以节省大量编写工作

now I found an expression like this 现在我发现这样的表达

   if ( ( isMax && lower-higher <= distance) ||
        ( !isMax && lower-higher >= distance) ) { ...

where 哪里
isMax is a bool defining if maximum (true) or minimum (false) is to be used, isMax是一个布尔值,用于定义要使用最大值(true)还是最小值(false),
lower and higher are the boundaries (int in this case) lowerhigher是边界(在这种情况下为int)

now i'm wondering: is there a way to somehow "pick" the operator this way? 现在我在想:是否有办法以这种方式“挑选”操作员?

I mean not the " x?a:b " way where the operand can be chosen, but to use a different operator 我的意思不是选择操作数的“ x?a:b”方式,而是使用其他运算符

something like bool (*op)() = isMax ? operator<=() : operator >= bool (*op)() = isMax ? operator<=() : operator >= bool (*op)() = isMax ? operator<=() : operator >= , used on lower-higher ? bool (*op)() = isMax ? operator<=() : operator >= ,用于lower-higher

or like lower-higher (isMax? <= : >=) distance , which won't work (of course) lower-higher (isMax? <= : >=) distance ,这是行不通的(当然)

Short answer: No. 简短答案:不可以。

But something close would be to write your own inline function with this effect: 但接近的结果是编写具有这种效果的内联函数:

template<class T>
inline bool compare(bool isLessThan, const T& left, const T& right)
{
    if (isLessThan) {
        return left <= right;
    }
    else {
        return left >= right;
    }
}

// ... later ...
if (compare(isMax, lower - higher, distance)) {
    // ...
}

My opinion (which you didn't ask for): just use an intermediate variable (or several if necessary)! 我的意见(您没有要求):仅使用一个中间变量(或必要时使用多个变量)!

You can do this if you encapsulate the operators into functions with the same type: 如果将运算符封装到相同类型的函数中,则可以执行以下操作:

namespace detail{
template<class T>
bool less_equal(T lhs, T rhs)
{
    std::cout << "Using <=\n";
    return lhs <= rhs;
}

template<class T>
bool greater_equal(T lhs, T rhs)
{
    std::cout << "Using >=\n";
    return lhs >= rhs;
}
}

And then we can write your logic as: 然后我们可以将您的逻辑写为:

void DoTheThing(bool isMax, int lower, int higher, int distance)
{
    auto func = isMax ? &detail::less_equal<int> : &detail::greater_equal<int>;
    if (func(lower-higher, distance))
    {
        // your logic here
    }
}

And a test: 并进行测试:

int main()
{
    DoTheThing(true, 1, 1, 1); // <=
    DoTheThing(false, 1, 1, 1); // >=
    return 0;
}

Output: 输出:

Using <=
Using >=

Demo 演示版

I'm not sure the ternary conditional is of much help here. 我不确定三元条件在这里是否有帮助。 But you can save some typing (and perhaps even gain a bit of performance; profile it) by writing 但是您可以通过编写一些代码来节省打字(甚至可能会获得一些性能;对其进行概要分析)

(2 * isMax - 1) * (lower - higher) <= distance

This assums that isMax is a bool type, or is either 1 or 0. A perfect equivalent is the obtuse 假设isMaxbool类型,或者是1或0。完美的等价项是钝角

(2 * !!isMax - 1) * (lower - higher) <= distance

On the grounds it took me 3 edits to get this correct, it might be encroaching on the borderline of readability. 从根本上讲,我花了3次编辑才能纠正此错误,它可能会侵犯可读性的界限。

Perhaps therefore leave it as it is, or bury the complexity in a function. 因此,也许可以原样保留它,或者将复杂性埋在函数中。

Apparently this is possible 1 : 显然这是可能的 1

(x ? [](int a, int b){ return a >= b; } : [](int a, int b){ return a <= b; })(a, b)

Should you do that, though? 不过,您应该这样做吗? I'd personally just go with a (bool, int, int) function. 我个人只是使​​用(bool, int, int)函数。

1 I was a bit surprised at first, but I guess it's triggering the lambda decay somehow. 1起初我有点惊讶,但是我猜想它以某种方式触发了lambda衰减。 If they were closures (had something within [] ) they would be different types and as such the type of the ?: would depend on runtime state. 如果它们是闭包( []内有东西),它们将是不同的类型,因此?:的类型将取决于运行时状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM