What is an equivalent replacement for std::unary_function in C++17?(C++17中std::unary_function的等效替代是什么?)
问题描述
以下代码给我带来了一些问题,尝试构建并得到错误:
"unary_function基类未定义"并且"unary_function"不是std的成员"
std::unary_function
已在C++17中删除,那么等效版本是什么?
#include <functional>
struct path_sep_comp: public std::unary_function<tchar, bool>
{
path_sep_comp () {}
bool
operator () (tchar ch) const
{
#if defined (_WIN32)
return ch == LOG4CPLUS_TEXT ('\') || ch == LOG4CPLUS_TEXT ('/');
#else
return ch == LOG4CPLUS_TEXT ('/');
#endif
}
};
推荐答案
std::unary_function
和许多其他基类(如std::not1
、std::binary_function
或std::iterator
)已逐渐弃用并从标准库中删除,因为不需要它们。
在现代C++中,正在使用概念。类是否专门从std::unary_function
继承并不重要,重要的是它有一个接受一个参数的调用操作符。这就是它是一元函数的原因。您可以通过将std::is_invocable
等特征与C++20中的SFINAE或requires
结合使用来检测到这一点。
在您的示例中,您只需从std::unary_function
:
struct path_sep_comp
{
// also note the removed default constructor, we don't need that
// we can make this constexpr in C++17
constexpr bool operator () (tchar ch) const
{
#if defined (_WIN32)
return ch == LOG4CPLUS_TEXT ('\') || ch == LOG4CPLUS_TEXT ('/');
#else
return ch == LOG4CPLUS_TEXT ('/');
#endif
}
};
这篇关于C++17中std::unary_function的等效替代是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++17中std::unary_function的等效替代是什么?


基础教程推荐
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09