如何解决为什么std :: ranges :: views :: filter无法将std :: isupper作为参数?
考虑以下代码:
#include <cctype>
#include <ranges>
constexpr inline auto filter_upper = std::views::filter(::isupper);
新的范围适配器filter_upper
与全局旧c函数::isupper
可以很好地工作,但是如果我替换为std::isupper
,则会出现此编译器错误:
<source>:4:69: error: no match for call to '(const std::ranges::views::__adaptor::_RangeAdaptor<std::ranges::views::<lambda(_Range&&,_Pred&&)> >) (<unresolved overloaded function type>)'
4 | constexpr inline auto filter_upper = std::views::filter(std::isupper);
| ^
In file included from <source>:1:
/opt/compiler-explorer/gcc-10.2.0/include/c++/10.2.0/ranges:1102:4: note: candidate: 'constexpr auto std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()(_Args&& ...) const [with _Args = {}; _Callable = std::ranges::views::<lambda(_Range&&,_Pred&&)>]'
1102 | operator()(_Args&&... __args) const
| ^~~~~~~~
问题出在哪里?
解决方法
在isupper
命名空间中,std
似乎有多个重载,编译器不知道该选择哪个。如果您使用static_cast<int (*)(int)>(std::isupper)
,它将编译。
无论如何,我相信您不允许使用std
函数的地址,因此最好使用lambda:
constexpr inline auto filter_upper = std::views::filter([](unsigned char c) { return std::isupper(c); });
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。