1 | #include <array> |
2 | #include <span> |
3 | #include <stdio.h> |
4 | #include <string> |
5 | #include <vector> |
6 | |
7 | template <class T, size_t N> |
8 | void by_ref_and_ptr(std::span<T, N> &ref, std::span<T, N> *ptr) { |
9 | printf("Stop here to check by ref" ); |
10 | return; |
11 | } |
12 | |
13 | int main() { |
14 | std::array numbers = {1, 12, 123, 1234, 12345}; |
15 | |
16 | using dynamic_string_span = std::span<std::string>; |
17 | |
18 | // Test span of ints |
19 | |
20 | // Full view of numbers with static extent |
21 | std::span numbers_span = numbers; |
22 | |
23 | printf(format: "break here" ); |
24 | |
25 | by_ref_and_ptr(numbers_span, &numbers_span); |
26 | |
27 | // Test spans of strings |
28 | std::vector<std::string> strings{"goofy" , "is" , "smart" , "!!!" }; |
29 | strings.reserve(n: strings.size() + 1); |
30 | |
31 | // Partial view of strings with dynamic extent |
32 | dynamic_string_span strings_span{std::span{strings}.subspan(2)}; |
33 | |
34 | auto strings_span_it = strings_span.begin(); |
35 | |
36 | printf(format: "break here" ); |
37 | |
38 | // Vector size doesn't increase, span should |
39 | // print unchanged and the strings_span_it |
40 | // remains valid |
41 | strings.emplace_back(args: "???" ); |
42 | |
43 | printf(format: "break here" ); |
44 | |
45 | // Now some empty spans |
46 | std::span<int, 0> static_zero_span; |
47 | std::span<int> dynamic_zero_span; |
48 | |
49 | // Multiple spans |
50 | std::array span_arr{strings_span, strings_span}; |
51 | std::span<std::span<std::string>, 2> nested = span_arr; |
52 | |
53 | printf(format: "break here" ); |
54 | |
55 | return 0; // break here |
56 | } |
57 | |