c++ - 什么是 C++17 中 std::unary_function 的等效替代品?
问题描述
这是导致我出现一些问题的代码,试图构建并得到错误:
'unary_function base class undefined' 和 '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
orstd::binary_function
或std::iterator
已逐渐弃用并从标准库中删除,因为不需要它们。
在现代 C++ 中,正在使用概念。一个类是否专门继承自std::unary_function
无关紧要,重要的是它有一个带有一个参数的调用运算符。这就是使它成为一元函数的原因。std::is_invocable
您可以通过结合SFINAE或requires
在 C++20 中使用特征来检测这一点。
在您的示例中,您可以简单地从以下位置删除继承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
}
};
推荐阅读
- coq - Coq 中的空集函数
- clojure - 如何在重新框架模板中运行后端服务器/代码?
- asp.net-mvc - 如何在 ActionFilterAttribute 中注入依赖
- swift - 如何在 Swift 中每年重复倒计时到某个日期
- node.js - 如何在 Azure 应用服务应用中查看 console.log() 输出
- artifactory - JFrog REST api 客户端应用程序无法构建缺少 org.apache.commons.* 包
- javascript - Javascript 如何从 HTML 表格中生成的单元格中获取值?
- ruby - 仅当我的 jar 文件在厨师中更新时如何解压文件?
- python - 将值列表转换为数据框时结果不正确
- javascript - 父窗口未从 Microsoft Edge 中的子窗口更新