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的等效替代是什么?


- XML Schema 到 C++ 类 2022-01-01
- 将 hdc 内容复制到位图 2022-09-04
- 将函数的返回值分配给引用 C++? 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- GDB 不显示函数名 2022-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- OpenGL 对象的 RAII 包装器 2021-01-01