我如何用其他没有痛苦的东西代替std :: binary_function

How do I replace std::binary_function with something else without pain?

本文关键字:std binary function 其他 何用 痛苦      更新时间:2023-10-16

我们当前正在使用一些使用某些std :: binary_function,std :: unary_function的第三方软件包。如您所知,这些功能已在C 14中弃用,现在它们都从C 17中删除了。我们将使用C 17的一些新功能,同时我们不会做出一些重大更改,因为这可能会导致代码中的某些不稳定。我们如何简单地用其他疼痛较小的东西替换这些遗产C 功能(std :: binary_function,...(。

预先感谢您的帮助。

我不知道标准库中的任何现有类型,但是创建自己的东西并不重要:

template<class Arg1, class Arg2, class Result> 
struct binary_function
{
    using first_argument_type = Arg1;
    using second_argument_type = Arg2;
    using result_type = Result;
};
template <typename ArgumentType, typename ResultType>
struct unary_function
{
    using argument_type = ArgumentType;
    using result_type = ResultType;
};

这两个类都是用户定义的功能对象的简单基类,例如:

struct MyFuncObj : std::unary_function<int, bool>
{
    bool operator()(int arg) { ... }
};

具有允许参数的别名使用某些内置功能中内置的标准库,例如std::not1std::not1(MyFuncObj())

我的猜测为什么不弃用是因为在C 11之后,大多数是使用lambdas来创建功能对象。并且具有variadic模板非常容易创建not和其他事物的通用版本,而没有std::not1std::not2