// -*- C++ -*- //===------------------------------ span ---------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===---------------------------------------------------------------------===// // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 // // template // constexpr span(Container& cont); // template // constexpr span(const Container& cont); // // Remarks: These constructors shall not participate in overload resolution unless: // — Container is not a specialization of span, // — Container is not a specialization of array, // — is_array_v is false, // — data(cont) and size(cont) are both well-formed, and // — remove_pointer_t(*)[] is convertible to ElementType(*)[]. // #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 const *getV() const {return &v_;} // for checking T v_; }; void checkCV() { std::vector v = {1,2,3}; // Types the same (dynamic sized) { std::span< int> s1{v}; // a span< int> pointing at int. } // Types the same (static sized) { std::span< int,3> s1{v}; // a span< int> pointing at int. } // types different (dynamic sized) { 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. } // types different (static sized) { std::span s1{v}; // a span pointing at int. std::span< volatile int,3> s2{v}; // a span< volatile int> pointing at int. std::span< volatile int,3> s3{v}; // a span< volatile int> pointing at const int. std::span s4{v}; // a span pointing at int. } } template constexpr bool testConstexprSpan() { constexpr IsAContainer val{}; std::span s1{val}; std::span s2{val}; return s1.data() == val.getV() && s1.size() == 1 && s2.data() == val.getV() && s2.size() == 1; } template void testRuntimeSpan() { IsAContainer val{}; std::span s1{val}; std::span s2{val}; assert(s1.data() == val.getV() && s1.size() == 1); assert(s2.data() == val.getV() && s2.size() == 1); } struct A{}; int main () { static_assert(testConstexprSpan(), ""); static_assert(testConstexprSpan(), ""); static_assert(testConstexprSpan(), ""); static_assert(testConstexprSpan(), ""); testRuntimeSpan(); testRuntimeSpan(); testRuntimeSpan(); testRuntimeSpan(); testRuntimeSpan(); checkCV(); }