gpt4 book ai didi

c++ - C++17 中 std::unary_function 的等效替代品是什么?

转载 作者:行者123 更新时间:2023-12-03 06:49:35 24 4
gpt4 key购买 nike

这是导致我出现一些问题的代码,尝试构建并获取错误:

'unary_function base class undefined' and 'unary_function' is not a member of 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::not1std::binary_functionstd::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
}
};

关于c++ - C++17 中 std::unary_function 的等效替代品是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63577103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com