替代std :: binary_function [英] replacement for std::binary_function

查看:440
本文介绍了替代std :: binary_function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: binary_function 现在已弃用,并将在。我搜索了其他出版物,但找不到替换它的确切方法。我想知道如何在样式。

std::binary_function is deprecated now and is going to be deleted in c++17. I searched on different publications, but I couldn't find a exact way to replace it. I'd like to know how should I write the following code in c++11 style.

template <class T>
inline T absolute(const T &x) {
    return (x >= 0) ? x : -x;
}

template <class T>
struct absoluteLess : public std::binary_function<T, T, bool> {
    bool operator()(const T &x, const T &y) const {
        return absolute(x) < absolute(y);
    }
};

template <class T>
struct absoluteGreater : public std::binary_function<T, T, bool> {
    bool operator()(T &x, T &y) const {
        return absolute(x) > absolute(y);
    }
};


EDIT


我通过以下方式使用这些功能:

EDIT

I'm using the functions in the following way:

output[j] = *std::max_element(input.begin() + prev,
                              input.begin() + pos,
                              absoluteLess<float>());

输入输出 std :: vector s,位于for循环内。

input and output are std::vectors, inside a for-loop.

推荐答案

首先,我的建议是观看 CppCon 2015:Stephan T. Lavavej功能:新增功能和正确用法 。在幻灯片36中提到了 std :: binary_function ,大约在视频中播放了36分钟。您可以在github.com/CppCon/CppCon2015上找到幻灯片。它并没有详细说明为什么您不应该使用 std :: binary_function 的原因,但是如果您使用的是自C ++ 11起就已弃用的东西,那么您会

First, my advice is to watch CppCon 2015: Stephan T. Lavavej "functional: What's New, And Proper Usage". std::binary_function is mentioned on slide 36, at around 36 mins in the video. You can find the slides at github.com/CppCon/CppCon2015). It doesn't go into detail why you shouldn't use std::binary_function, but if you're using something that's been deprecated since C++11, then you would probably benefit from watching it.

如果您想要不使用它的实际理由,请尝试n4190:

If you want the actual rationale for not using it, try n4190:

当C ++ 98年代的
适配器需要arguments_type / etc时,

unary_function / binary_function是有用的帮助器。 typedefs。鉴于C ++ 11的完美转发,decltype等,此类typedef是
不必要的。
(而且它们不适用于重载/模板化函数调用
运算符。)即使一个类想提供这些typedef来实现
的向后兼容性,它也可以直接这样做(费用不高)以
的详细程度表示),而不是从unary_function / binary_function继承而来,
是标准本身在不赞成使用这些助手
时开始做的事情。

unary_function/binary_function were useful helpers when C++98-era adaptors needed argument_type/etc. typedefs. Such typedefs are unnecessary given C++11's perfect forwarding, decltype, and so forth. (And they're inapplicable to overloaded/templated function call operators.) Even if a class wants to provide these typedefs for backwards compatibility, it can do so directly (at a minor cost in verbosity) instead of inheriting from unary_function/binary_function, which is what the Standard itself started doing when these helpers were deprecated.

现在您根本不需要它,因此您可以从程序中删除所有痕迹。

Now you simply don't need it, so you can remove all traces of it from your program.

在C ++中14,增加了透明比较器。但是可以在C ++ 11中实现。只需将其专门用于 void

In C++14, transparent comparators were added. But it can be implemented in C++11. Just specialize it for void:

template<>
struct absoluteLess<void> {
    template< class T, class U>
    constexpr auto operator()( T&& lhs, U&& rhs ) const
      -> decltype(absolute(std::forward<T>(lhs)) < absolute(std::forward<U>(rhs)))
    {
        return absolute(std::forward<T>(lhs)) < absolute(std::forward<U>(rhs));
    }
}
};

现在可以推导类型:

std::max_element(v.begin(), v.end(), absoluteLess<>());

这篇关于替代std :: binary_function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆