我们目前正在使用一些第三方软件包,其中使用一些std::binary_function,std::unary_function的深处。您可能知道,这些函数在C++14中已经被废弃,现在它们都从C++17中删除了。我们将使用C++17的一些新特性,同时我们不会做一些重大更改,因为它可能会导致代码中的一些不稳定。我们如何简单地替换这些遗留的C++特性(std::binary_function,.)其他痛苦较小的东西。
提前谢谢你的帮助。
发布于 2019-05-06 08:24:01
我不知道标准库中的任何现有类型,但是创建自己的库并不是什么大事:
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。
https://stackoverflow.com/questions/56001160
复制相似问题