下面是引起我的一些问题的代码,试图构建并获取错误:
'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
}
};
发布于 2020-08-25 11:06:06
std::unary_function
和许多其他基类(如std::not1
、std::binary_function
或std::iterator
)已经逐渐被废弃,并从标准库中删除,因为它们不需要它们。
在现代C++中,概念正在被使用。一个类是否具体从std::unary_function
继承并不重要,重要的是它有一个带有一个参数的调用运算符。这就是使它成为一元函数的原因。您将通过使用std::is_invocable
、或C++20中的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
}
};
https://stackoverflow.com/questions/63577103
复制相似问题