I have the following code, which I expected to work, but it doesn't:
#include <array>
#include <span>
auto f = std::span<int>(std::array<int, 3>{});
clang fails to compile this:
error: no matching conversion for functional-style cast from 'std::array<int, 3>' to 'std::span<int>'
auto f = std::span<int>(std::array<int, 3>{});
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: candidate template ignored: constraints not satisfied [with _Tp = int, _ArrayExtent = 3]
span(const array<_Tp, _ArrayExtent>& __arr) noexcept
^
I expected this to call the constructor:
template< class U, std::size_t N > constexpr span( const std::array<U, N>& arr ) noexcept; // (6)
4-6) Constructs a span that is a view over the array arr; the resulting span has
size() == N
anddata() == std::data(arr)
.These overloads participate in overload resolution only if
extent == std::dynamic_extent || N == extent
istrue
and the conversion fromstd::remove_pointer_t<decltype(data(arr))>
toelement_type
is at most a qualification conversion.
See std::span::span
on cppreference
Why are the constraints of this constructor not satisfied?
extent == std::dynamic_extent
for astd::span<int>
, so the first requirement is obviously metstd::remove_pointer_t<decltype(data(arr))>
isint
, which is equal tostd::span<int>::element_type = int
, so the second requirement is also met
I don't see any reason why I wouldn't be able to call this constructor.