[Bug libstdc++/101091] std::views::take and std::views::drop are overconstrained

j.galecki11 at gmail dot com gcc-bugzilla@gcc.gnu.org
Wed Jun 16 10:54:37 GMT 2021


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

--- Comment #1 from Jakub Gałecki <j.galecki11 at gmail dot com> ---
Consider the following example: https://godbolt.org/z/YYzqWaM9G

#include <algorithm>
#include <ranges>
#include <vector>

void copy_drop_n1(const std::vector<double>& src, std::vector<double>& dest,
                  std::size_t n) {  // size_t - narrowing conversion
  std::ranges::copy(src | std::views::drop(n), begin(dest) + n);
}

void copy_drop_n2(const std::vector<double>& src, std::vector<double>& dest,
                  std::ptrdiff_t n) {  // ptrdiff_t
  std::ranges::copy(src | std::views::drop(n), begin(dest) + n);
}

As per the standard [range.take.overview]:
The name views​::​take denotes a range adaptor object ([range.adaptor.object]).
Let E and F be expressions, let T be remove_­cvref_­t<decltype((E))>, and let D
be range_­difference_­t<decltype((E))>. If decltype((F)) does not model
convertible_­to<D>, views​::​take(E, F) is ill-formed. Otherwise, the
expression views​::​take(E, F) is expression-equivalent to [...]

The example above should therefore compile for size_t as well as ptrdiff_t,
since size_t is convertible to ptrdiff_t. This is a regression from libstdc++
version 10.x, where both functions compile.


More information about the Gcc-bugs mailing list