// -*- C++ -*- //===------------------------------ span ---------------------------------===// // // 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 span(Container& cont); // template // constexpr span(const Container& cont); // // Remarks: These constructors shall not participate in overload resolution unless: // — extent == dynamic_extent, // — 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 #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; }