4

考虑以下代码:

#include <cctype>
#include <ranges>

constexpr inline auto filter_upper = std::views::filter(::isupper);

新的范围适配器filter_upper适用于全局旧 c-function ::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
      |    ^~~~~~~~

问题出在哪里?

4

1 回答 1

3

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); });
于 2020-10-14T10:47:28.247 回答