【发布时间】:2015-10-14 00:15:07
【问题描述】:
std::binary_function 现在已弃用,将在c++17 中删除。我搜索了不同的出版物,但我找不到替换它的确切方法。我想知道我应该如何以c++11风格编写以下代码。
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);
}
};
编辑
我正在通过以下方式使用这些功能:
output[j] = *std::max_element(input.begin() + prev,
input.begin() + pos,
absoluteLess<float>());
input 和 output 是 std::vectors,在 for 循环中。
【问题讨论】:
-
为什么需要它? type traits 和 decltype 可以在不需要
binary_function的情况下找出operator()的类型 -
如何使用这些模板功能?答案取决于使用情况。
-
stackoverflow.com/a/22863957/642626 这就是你如何找出任何可调用对象的参数和返回类型。
-
LLVM C++ 标准库在 2020 年底仍然使用
binary_functiongithub.com/llvm/llvm-project/blob/main/libcxx/include/… 更重要的是clang++ --std=c++17仍然可以编译使用binary_functiongithub.com/llvm/llvm-project/blob/main/libcxx/include/… 的代码
标签: c++17 c++11 c++ c++11 deprecated