如何用其他东西替换 std::binary_function 而不痛苦?

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

我们目前正在使用一些 3rd 方包,这些包在内部使用了一些 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::not1: std::not1(MyFuncObj()).

我猜这被弃用的原因是因为在 C++11 之后,大多数 lambda 用于创建函数对象。有了可变参数模板,就可以很容易地创建 not 和其他东西的通用版本,而无需 std::not1std::not2.