https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99433

--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
(In reply to gcc-bugs from comment #5)
> Thank you for the fix, but the following code does not compile any more:
> 
> ```c++
> #include <list>
> #include <ranges>
> 
> int main()
> {
>   std::list<char> list;
> 
>   constexpr auto drop = []<std::ranges::viewable_range urng_t>(urng_t &&
> urange, size_t drop_size)
>   {
>     // does not work:
>     return std::forward<urng_t>(urange) | std::views::drop(drop_size);
> 
>     // does work:
>     // return std::forward<urng_t>(urange) | std::views::drop(0);
>   };
>   drop(list, 0);
> }
> ```
> 
> Should I open a new issue?

Reduced:

#include <list>
#include <ranges>

int main() {
  std::list<char> list;
  std::views::drop(list, 0ul);
}

The constraint satisfaction failure diagnostic says:

libstdc++-v3/include/std/ranges:2100:10:   required for the satisfaction of
‘__can_drop_view<_Range, _Tp>’ [with _Range = std::__cxx11::list<char,
std::allocator<char> >&; _Tp = lon
g unsigned int]
libstdc++-v3/include/std/ranges:2101:6:   in requirements  [with _Tp = long
unsigned int; _Range = std::__cxx11::list<char, std::allocator<char> >&]
libstdc++-v3/include/std/ranges:2101:24: note: the required expression
‘drop_view<...auto...>{declval<_Range>(), declval<_Tp>()}’ is invalid, because
 2101 |           = requires { drop_view{std::declval<_Range>(),
std::declval<_Tp>()}; };
      |                       
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

but it strangely doesn't explain why the expression is invalid.  Turns out if
we pass -Wsystem-headers to the command line, then we do get an explanation in
the form of a warning:

<snip>
libstdc++-v3/include/std/ranges:2101:24: warning: narrowing conversion of
‘std::declval<long unsigned int>()’ from ‘long unsigned int’ to
‘std::ranges::range_difference_t<std::ranges::ref_view<std::__cxx11::list<char>
> >’ {aka ‘long int’} [-Wnarrowing]

So I suppose we're correct to reject the testcase, since narrowing conversions
aren't permitted in braced init lists (and views​::​drop(E, F) is specified to
be expression-equivalent to the braced init ranges​::​drop_­view{E, F}).

But it's perhaps a frontend bug that we need to pass -Wsystem-headers to get
the warning here in the first place; I'll file a PR for this issue.

Reply via email to