在三元组中使用std :: greater <K>和std :: greater_equal <K>

P45*_*ent 1 c++ c++11

我正在使用std::find_if并且作为第三个参数我想在其中使用三元组std::bind2nd:

std::bind2nd(foo ? std::greater<K>() : std::greater_equal<K>(), bar);
Run Code Online (Sandbox Code Playgroud)

对于某种类型K.foobool一面旗帜.但是,这不是因为syntatically正确的std::greater<K>(),并std::greater_equal<K>()有不同的类型.

理想情况下,我想删除三元(因为上面的循环运行各种bars)并且有

auto predicate = foo ? std::greater<K>() : std::greater_equal<K>();
std::bind2nd(predicate, bar); /*this statement is in a loop*/
Run Code Online (Sandbox Code Playgroud)

但这不合法,因为predicate在编译时不知道类型.但是std::bind2nd可以采取任何一种类型,所以我的想法是可以有一种方法来实现我想要的.在那儿?

Pio*_*cki 6

制作相同类型的操作数:

auto predicate = foo
             ? std::function<bool(K,K)>(std::greater<K>())
             : std::function<bool(K,K)>(std::greater_equal<K>());
Run Code Online (Sandbox Code Playgroud)

DEMO

理由:

§5.16条件运算符[expr.cond]/p3

[...]如果第二个和第三个操作数具有不同的类型并且具有(可能是cv限定的)类类型,或者如果两者都是相同值类别的glvalues和除cv-qualification之外的相同类型,则尝试将每个操作数转换为另一个操作数的类型.

之间不存在转换std::greater<K>()std::greater_equal<K>()一个编译器可以自行进行.