File: container.fail.cpp

package info (click to toggle)
llvm-toolchain-7 1%3A7.0.1-8~deb9u3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 733,456 kB
  • sloc: cpp: 3,776,651; ansic: 633,271; asm: 350,301; python: 142,716; objc: 107,612; sh: 22,626; lisp: 11,056; perl: 7,999; pascal: 6,742; ml: 5,537; awk: 3,536; makefile: 2,557; cs: 2,027; xml: 841; ruby: 156
file content (117 lines) | stat: -rw-r--r-- 6,595 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// -*- 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 

// <span>

//  template<class Container>
//     constexpr span(Container& cont);
//   template<class Container>
//     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<Container> is false,
//   — data(cont) and size(cont) are both well-formed, and
//   — remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[].
//


#include <span>
#include <cassert>
#include <list>
#include <forward_list>
#include <deque>

#include "test_macros.h"

//  Look ma - I'm a container!
template <typename T>
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 <typename T>
struct NotAContainerNoData {
    size_t size() const {return 0;}
};

template <typename T>
struct NotAContainerNoSize {
    const T *data() const {return nullptr;}
};

template <typename T>
struct NotAContainerPrivate {
private:
    size_t size() const {return 0;}
    const T *data() const {return nullptr;}
};


int main ()
{

//  Missing size and/or data
    {
    std::span<int>    s1{IsAContainer<int>()};          // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
    std::span<int, 0> s2{IsAContainer<int>()};          // expected-error {{no matching constructor for initialization of 'std::span<int, 0>'}}
    std::span<int>    s3{NotAContainerNoData<int>()};   // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
    std::span<int, 0> s4{NotAContainerNoData<int>()};   // expected-error {{no matching constructor for initialization of 'std::span<int, 0>'}}
    std::span<int>    s5{NotAContainerNoSize<int>()};   // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
    std::span<int, 0> s6{NotAContainerNoSize<int>()};   // expected-error {{no matching constructor for initialization of 'std::span<int, 0>'}}
    std::span<int>    s7{NotAContainerPrivate<int>()};  // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
    std::span<int, 0> s8{NotAContainerPrivate<int>()};  // expected-error {{no matching constructor for initialization of 'std::span<int, 0>'}}

//  Again with the standard containers
    std::span<int>    s11{std::deque<int>()};           // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
    std::span<int, 0> s12{std::deque<int>()};           // expected-error {{no matching constructor for initialization of 'std::span<int, 0>'}}
    std::span<int>    s13{std::list<int>()};            // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
    std::span<int, 0> s14{std::list<int>()};            // expected-error {{no matching constructor for initialization of 'std::span<int, 0>'}}
    std::span<int>    s15{std::forward_list<int>()};    // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
    std::span<int, 0> s16{std::forward_list<int>()};    // expected-error {{no matching constructor for initialization of 'std::span<int, 0>'}}
    }

//  Not the same type
    {
    std::span<float>    s1{IsAContainer<int>()};   // expected-error {{no matching constructor for initialization of 'std::span<float>'}}
    std::span<float, 0> s2{IsAContainer<int>()};   // expected-error {{no matching constructor for initialization of 'std::span<float, 0>'}}    
    }

//  CV wrong (dynamically sized)
    {
    std::span<               int> s1{IsAContainer<const          int>()};   // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
    std::span<               int> s2{IsAContainer<      volatile int>()};   // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
    std::span<               int> s3{IsAContainer<const volatile int>()};   // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
    std::span<const          int> s4{IsAContainer<      volatile int>()};   // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
    std::span<const          int> s5{IsAContainer<const volatile int>()};   // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
    std::span<      volatile int> s6{IsAContainer<const          int>()};   // expected-error {{no matching constructor for initialization of 'std::span<volatile int>'}}
    std::span<      volatile int> s7{IsAContainer<const volatile int>()};   // expected-error {{no matching constructor for initialization of 'std::span<volatile int>'}}
    }

//  CV wrong (statically sized)
    {
    std::span<               int,1> s1{IsAContainer<const          int>()}; // expected-error {{no matching constructor for initialization of 'std::span<int, 1>'}}
    std::span<               int,1> s2{IsAContainer<      volatile int>()}; // expected-error {{no matching constructor for initialization of 'std::span<int, 1>'}}
    std::span<               int,1> s3{IsAContainer<const volatile int>()}; // expected-error {{no matching constructor for initialization of 'std::span<int, 1>'}}
    std::span<const          int,1> s4{IsAContainer<      volatile int>()}; // expected-error {{no matching constructor for initialization of 'std::span<const int, 1>'}}
    std::span<const          int,1> s5{IsAContainer<const volatile int>()}; // expected-error {{no matching constructor for initialization of 'std::span<const int, 1>'}}
    std::span<      volatile int,1> s6{IsAContainer<const          int>()}; // expected-error {{no matching constructor for initialization of 'std::span<volatile int, 1>'}}
    std::span<      volatile int,1> s7{IsAContainer<const volatile int>()}; // expected-error {{no matching constructor for initialization of 'std::span<volatile int, 1>'}}
    }

}