diff --git a/libcxx/include/span b/libcxx/include/span --- a/libcxx/include/span +++ b/libcxx/include/span @@ -170,7 +170,25 @@ template struct __is_std_span> : true_type {}; -#if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) +#if defined(_LIBCPP_HAS_NO_CONCEPTS) || defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) +// This is a temporary workaround until we ship -- we've unfortunately been +// shipping before its API was finalized, and we used to provide a constructor +// from container types that had the requirements below. To avoid breaking code that +// has started relying on the range-based constructor until we ship all of , +// we emulate the constructor requirements like this. +template +struct __span_compatible_range : false_type { }; + +template +struct __span_compatible_range<_Range, _ElementType, void_t< + enable_if_t>::value>, + enable_if_t>::value>, + enable_if_t>>, + decltype(data(declval<_Range>())), + decltype(size(declval<_Range>())), + enable_if_t()))>(*)[], _ElementType(*)[]>> +>> : true_type { }; +#else template concept __span_compatible_range = ranges::contiguous_range<_Range> && @@ -248,7 +266,22 @@ _LIBCPP_INLINE_VISIBILITY constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {} -#if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) +#if defined(_LIBCPP_HAS_NO_CONCEPTS) || defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + template ::value + >> + _LIBCPP_INLINE_VISIBILITY + constexpr explicit span(_Container& __c) : __data{std::data(__c)} { + _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)"); + } + template ::value + >> + _LIBCPP_INLINE_VISIBILITY + constexpr explicit span(const _Container& __c) : __data{std::data(__c)} { + _LIBCPP_ASSERT(std::size(__c) == _Extent, "size mismatch in span's constructor (range)"); + } +#else template <__span_compatible_range _Range> _LIBCPP_INLINE_VISIBILITY constexpr explicit span(_Range&& __r) : __data{ranges::data(__r)} { @@ -434,7 +467,18 @@ _LIBCPP_INLINE_VISIBILITY constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {} -#if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) +#if defined(_LIBCPP_HAS_NO_CONCEPTS) || defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + template ::value + >> + _LIBCPP_INLINE_VISIBILITY + constexpr span(_Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {} + template ::value + >> + _LIBCPP_INLINE_VISIBILITY + constexpr span(const _Container& __c) : __data(std::data(__c)), __size{std::size(__c)} {} +#else template <__span_compatible_range _Range> _LIBCPP_INLINE_VISIBILITY constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {} diff --git a/libcxx/test/libcxx/containers/views/span.cons/range.pass.cpp b/libcxx/test/libcxx/containers/views/span.cons/range.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/containers/views/span.cons/range.pass.cpp @@ -0,0 +1,141 @@ +//===---------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// + +// template +// constexpr explicit(Extent != dynamic_extent) span(Container&); +// template +// constexpr explicit(Extent != dynamic_extent) span(Container const&); + +// This test checks for libc++'s non-conforming temporary extension to std::span +// to support construction from containers that look like contiguous ranges. +// +// This extension is only supported when we don't ship , and we can +// remove it once we get rid of _LIBCPP_HAS_NO_INCOMPLETE_RANGES. + +#include +#include +#include +#include + +#include "test_macros.h" + +// Look ma - I'm a container! +template +struct IsAContainer { + constexpr IsAContainer() : v_{} {} + constexpr size_t size() const {return 1;} + constexpr T *data() {return &v_;} + constexpr const T *data() const {return &v_;} + constexpr T *begin() {return &v_;} + constexpr const T *begin() const {return &v_;} + constexpr T *end() {return &v_ + 1;} + constexpr const T *end() const {return &v_ + 1;} + + constexpr T const *getV() const {return &v_;} // for checking + T v_; +}; + + +void checkCV() +{ + std::vector v = {1,2,3}; + +// Types the same + { + std::span< int> s1{v}; // a span< int> pointing at int. + } + +// types different + { + std::span s1{v}; // a span pointing at int. + std::span< volatile int> s2{v}; // a span< volatile int> pointing at int. + std::span< volatile int> s3{v}; // a span< volatile int> pointing at const int. + std::span s4{v}; // a span pointing at int. + } + +// Constructing a const view from a temporary + { + std::span s1{IsAContainer()}; + std::span s3{std::vector()}; + (void) s1; + (void) s3; + } +} + + +template +constexpr bool testConstexprSpan() +{ + constexpr IsAContainer val{}; + std::span s1{val}; + return s1.data() == val.getV() && s1.size() == 1; +} + +template +constexpr bool testConstexprSpanStatic() +{ + constexpr IsAContainer val{}; + std::span s1{val}; + return s1.data() == val.getV() && s1.size() == 1; +} + +template +void testRuntimeSpan() +{ + IsAContainer val{}; + const IsAContainer cVal; + std::span s1{val}; + std::span s2{cVal}; + assert(s1.data() == val.getV() && s1.size() == 1); + assert(s2.data() == cVal.getV() && s2.size() == 1); +} + +template +void testRuntimeSpanStatic() +{ + IsAContainer val{}; + const IsAContainer cVal; + std::span s1{val}; + std::span s2{cVal}; + assert(s1.data() == val.getV() && s1.size() == 1); + assert(s2.data() == cVal.getV() && s2.size() == 1); +} + +struct A{}; + +int main(int, char**) +{ + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + static_assert(testConstexprSpan(), ""); + + static_assert(testConstexprSpanStatic(), ""); + static_assert(testConstexprSpanStatic(), ""); + static_assert(testConstexprSpanStatic(), ""); + static_assert(testConstexprSpanStatic(), ""); + + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + testRuntimeSpan(); + + testRuntimeSpanStatic(); + testRuntimeSpanStatic(); + testRuntimeSpanStatic(); + testRuntimeSpanStatic(); + testRuntimeSpanStatic(); + + checkCV(); + + return 0; +} diff --git a/libcxx/test/libcxx/containers/views/span.cons/range.verify.cpp b/libcxx/test/libcxx/containers/views/span.cons/range.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/containers/views/span.cons/range.verify.cpp @@ -0,0 +1,118 @@ +//===---------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===---------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// + +// template +// constexpr explicit(Extent != dynamic_extent) span(Container&); +// template +// constexpr explicit(Extent != dynamic_extent) span(Container const&); + +// This test checks for libc++'s non-conforming temporary extension to std::span +// to support construction from containers that look like contiguous ranges. +// +// This extension is only supported when we don't ship , and we can +// remove it once we get rid of _LIBCPP_HAS_NO_INCOMPLETE_RANGES. + +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" + +// Look ma - I'm a container! +template +struct IsAContainer { + constexpr IsAContainer() : v_{} {} + constexpr size_t size() const {return 1;} + constexpr T *data() {return &v_;} + constexpr const T *data() const {return &v_;} + + constexpr const T *getV() const {return &v_;} // for checking + T v_; +}; + +template +struct NotAContainerNoData { + size_t size() const {return 0;} +}; + +template +struct NotAContainerNoSize { + const T *data() const {return nullptr;} +}; + +template +struct NotAContainerPrivate { +private: + size_t size() const {return 0;} + const T *data() const {return nullptr;} +}; + +template +std::span createImplicitSpan(container c) { + return {c}; // expected-error {{chosen constructor is explicit in copy-initialization}} +} + +int main(int, char**) +{ + +// Making non-const spans from const sources (a temporary binds to `const &`) + { + std::span s1{IsAContainer()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s3{std::vector()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// Missing size and/or data + { + std::span s1{NotAContainerNoData()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s3{NotAContainerNoSize()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{NotAContainerPrivate()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + +// Again with the standard containers + std::span s11{std::deque()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s13{std::list()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s15{std::forward_list()}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// Not the same type + { + IsAContainer c; + std::span s1{c}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// CV wrong + { + IsAContainer c; + IsAContainer cv; + IsAContainer< volatile int> v; + + std::span< int> s1{c}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s2{v}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< int> s3{cv}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s4{v}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span s5{cv}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s6{c}; // expected-error {{no matching constructor for initialization of 'std::span'}} + std::span< volatile int> s7{cv}; // expected-error {{no matching constructor for initialization of 'std::span'}} + } + +// explicit constructor necessary + { + IsAContainer c; + const IsAContainer cc; + + createImplicitSpan(c); + createImplicitSpan(cc); + } + + return 0; +}