我们当前正在使用一些第三方软件包,这些软件包在内部使用了一些std :: binary_function和std :: unary_function。您可能知道,这些功能在C ++ 14中已被弃用,现在它们都已从C ++ 17中删除。我们将使用C ++ 17的一些新功能,同时我们也不会进行重大更改,因为这可能会导致我们的代码不稳定。我们如何简单地用痛苦更少的其他东西替换这些旧的C ++功能(std :: binary_function,...)。
预先感谢您的帮助。
答案 0 :(得分:1)
我不知道标准库中的任何现有类型,但是创建自己的类型并不重要:
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::not1
,就很容易创建std::not2
等通用版本。