-
Notifications
You must be signed in to change notification settings - Fork 44
/
span.hpp
1947 lines (1520 loc) · 53.7 KB
/
span.hpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// span for C++98 and later.
// Based on http://wg21.link/p0122r7
// For more information see https://github.com/martinmoene/span-lite
//
// Copyright 2018-2021 Martin Moene
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef NONSTD_SPAN_HPP_INCLUDED
#define NONSTD_SPAN_HPP_INCLUDED
#define span_lite_MAJOR 0
#define span_lite_MINOR 11
#define span_lite_PATCH 0
#define span_lite_VERSION span_STRINGIFY(span_lite_MAJOR) "." span_STRINGIFY(span_lite_MINOR) "." span_STRINGIFY(span_lite_PATCH)
#define span_STRINGIFY( x ) span_STRINGIFY_( x )
#define span_STRINGIFY_( x ) #x
// span configuration:
#define span_SPAN_DEFAULT 0
#define span_SPAN_NONSTD 1
#define span_SPAN_STD 2
// tweak header support:
#ifdef __has_include
# if __has_include(<nonstd/span.tweak.hpp>)
# include <nonstd/span.tweak.hpp>
# endif
#define span_HAVE_TWEAK_HEADER 1
#else
#define span_HAVE_TWEAK_HEADER 0
//# pragma message("span.hpp: Note: Tweak header not supported.")
#endif
// span selection and configuration:
#define span_HAVE( feature ) ( span_HAVE_##feature )
#ifndef span_CONFIG_SELECT_SPAN
# define span_CONFIG_SELECT_SPAN ( span_HAVE_STD_SPAN ? span_SPAN_STD : span_SPAN_NONSTD )
#endif
#ifndef span_CONFIG_EXTENT_TYPE
# define span_CONFIG_EXTENT_TYPE std::size_t
#endif
#ifndef span_CONFIG_SIZE_TYPE
# define span_CONFIG_SIZE_TYPE std::size_t
#endif
#ifdef span_CONFIG_INDEX_TYPE
# error `span_CONFIG_INDEX_TYPE` is deprecated since v0.7.0; it is replaced by `span_CONFIG_SIZE_TYPE`.
#endif
// span configuration (features):
#ifndef span_FEATURE_WITH_INITIALIZER_LIST_P2447
# define span_FEATURE_WITH_INITIALIZER_LIST_P2447 0
#endif
#ifndef span_FEATURE_WITH_CONTAINER
#ifdef span_FEATURE_WITH_CONTAINER_TO_STD
# define span_FEATURE_WITH_CONTAINER span_IN_STD( span_FEATURE_WITH_CONTAINER_TO_STD )
#else
# define span_FEATURE_WITH_CONTAINER 0
# define span_FEATURE_WITH_CONTAINER_TO_STD 0
#endif
#endif
#ifndef span_FEATURE_CONSTRUCTION_FROM_STDARRAY_ELEMENT_TYPE
# define span_FEATURE_CONSTRUCTION_FROM_STDARRAY_ELEMENT_TYPE 0
#endif
#ifndef span_FEATURE_MEMBER_AT
# define span_FEATURE_MEMBER_AT 0
#endif
#ifndef span_FEATURE_MEMBER_BACK_FRONT
# define span_FEATURE_MEMBER_BACK_FRONT 1
#endif
#ifndef span_FEATURE_MEMBER_CALL_OPERATOR
# define span_FEATURE_MEMBER_CALL_OPERATOR 0
#endif
#ifndef span_FEATURE_MEMBER_SWAP
# define span_FEATURE_MEMBER_SWAP 0
#endif
#ifndef span_FEATURE_NON_MEMBER_FIRST_LAST_SUB
# define span_FEATURE_NON_MEMBER_FIRST_LAST_SUB 0
#elif span_FEATURE_NON_MEMBER_FIRST_LAST_SUB
# define span_FEATURE_NON_MEMBER_FIRST_LAST_SUB_SPAN 1
# define span_FEATURE_NON_MEMBER_FIRST_LAST_SUB_CONTAINER 1
#endif
#ifndef span_FEATURE_NON_MEMBER_FIRST_LAST_SUB_SPAN
# define span_FEATURE_NON_MEMBER_FIRST_LAST_SUB_SPAN 0
#endif
#ifndef span_FEATURE_NON_MEMBER_FIRST_LAST_SUB_CONTAINER
# define span_FEATURE_NON_MEMBER_FIRST_LAST_SUB_CONTAINER 0
#endif
#ifndef span_FEATURE_COMPARISON
# define span_FEATURE_COMPARISON 0 // Note: C++20 does not provide comparison
#endif
#ifndef span_FEATURE_SAME
# define span_FEATURE_SAME 0
#endif
#if span_FEATURE_SAME && !span_FEATURE_COMPARISON
# error `span_FEATURE_SAME` requires `span_FEATURE_COMPARISON`
#endif
#ifndef span_FEATURE_MAKE_SPAN
#ifdef span_FEATURE_MAKE_SPAN_TO_STD
# define span_FEATURE_MAKE_SPAN span_IN_STD( span_FEATURE_MAKE_SPAN_TO_STD )
#else
# define span_FEATURE_MAKE_SPAN 0
# define span_FEATURE_MAKE_SPAN_TO_STD 0
#endif
#endif
#ifndef span_FEATURE_BYTE_SPAN
# define span_FEATURE_BYTE_SPAN 0
#endif
// Control presence of exception handling (try and auto discover):
#ifndef span_CONFIG_NO_EXCEPTIONS
# if defined(_MSC_VER)
# include <cstddef> // for _HAS_EXCEPTIONS
# endif
# if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS)
# define span_CONFIG_NO_EXCEPTIONS 0
# else
# define span_CONFIG_NO_EXCEPTIONS 1
# undef span_CONFIG_CONTRACT_VIOLATION_THROWS
# undef span_CONFIG_CONTRACT_VIOLATION_TERMINATES
# define span_CONFIG_CONTRACT_VIOLATION_THROWS 0
# define span_CONFIG_CONTRACT_VIOLATION_TERMINATES 1
# endif
#endif
// Control pre- and postcondition violation behaviour:
#if defined( span_CONFIG_CONTRACT_LEVEL_ON )
# define span_CONFIG_CONTRACT_LEVEL_MASK 0x11
#elif defined( span_CONFIG_CONTRACT_LEVEL_OFF )
# define span_CONFIG_CONTRACT_LEVEL_MASK 0x00
#elif defined( span_CONFIG_CONTRACT_LEVEL_EXPECTS_ONLY )
# define span_CONFIG_CONTRACT_LEVEL_MASK 0x01
#elif defined( span_CONFIG_CONTRACT_LEVEL_ENSURES_ONLY )
# define span_CONFIG_CONTRACT_LEVEL_MASK 0x10
#else
# define span_CONFIG_CONTRACT_LEVEL_MASK 0x11
#endif
#if defined( span_CONFIG_CONTRACT_VIOLATION_THROWS )
# define span_CONFIG_CONTRACT_VIOLATION_THROWS_V span_CONFIG_CONTRACT_VIOLATION_THROWS
#else
# define span_CONFIG_CONTRACT_VIOLATION_THROWS_V 0
#endif
#if defined( span_CONFIG_CONTRACT_VIOLATION_THROWS ) && span_CONFIG_CONTRACT_VIOLATION_THROWS && \
defined( span_CONFIG_CONTRACT_VIOLATION_TERMINATES ) && span_CONFIG_CONTRACT_VIOLATION_TERMINATES
# error Please define none or one of span_CONFIG_CONTRACT_VIOLATION_THROWS and span_CONFIG_CONTRACT_VIOLATION_TERMINATES to 1, but not both.
#endif
// C++ language version detection (C++23 is speculative):
// Note: VC14.0/1900 (VS2015) lacks too much from C++14.
#ifndef span_CPLUSPLUS
# if defined(_MSVC_LANG ) && !defined(__clang__)
# define span_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
# else
# define span_CPLUSPLUS __cplusplus
# endif
#endif
#define span_CPP98_OR_GREATER ( span_CPLUSPLUS >= 199711L )
#define span_CPP11_OR_GREATER ( span_CPLUSPLUS >= 201103L )
#define span_CPP14_OR_GREATER ( span_CPLUSPLUS >= 201402L )
#define span_CPP17_OR_GREATER ( span_CPLUSPLUS >= 201703L )
#define span_CPP20_OR_GREATER ( span_CPLUSPLUS >= 202002L )
#define span_CPP23_OR_GREATER ( span_CPLUSPLUS >= 202300L )
// C++ language version (represent 98 as 3):
#define span_CPLUSPLUS_V ( span_CPLUSPLUS / 100 - (span_CPLUSPLUS > 200000 ? 2000 : 1994) )
#define span_IN_STD( v ) ( ((v) == 98 ? 3 : (v)) >= span_CPLUSPLUS_V )
#define span_CONFIG( feature ) ( span_CONFIG_##feature )
#define span_FEATURE( feature ) ( span_FEATURE_##feature )
#define span_FEATURE_TO_STD( feature ) ( span_IN_STD( span_FEATURE( feature##_TO_STD ) ) )
// Use C++20 std::span if available and requested:
#if span_CPP20_OR_GREATER && defined(__has_include )
# if __has_include( <span> )
# define span_HAVE_STD_SPAN 1
# else
# define span_HAVE_STD_SPAN 0
# endif
#else
# define span_HAVE_STD_SPAN 0
#endif
#define span_USES_STD_SPAN ( (span_CONFIG_SELECT_SPAN == span_SPAN_STD) || ((span_CONFIG_SELECT_SPAN == span_SPAN_DEFAULT) && span_HAVE_STD_SPAN) )
//
// Use C++20 std::span:
//
#if span_USES_STD_SPAN
#include <span>
namespace nonstd {
using std::span;
using std::dynamic_extent;
// Note: C++20 does not provide comparison
// using std::operator==;
// using std::operator!=;
// using std::operator<;
// using std::operator<=;
// using std::operator>;
// using std::operator>=;
} // namespace nonstd
#else // span_USES_STD_SPAN
#include <algorithm>
// Compiler versions:
//
// MSVC++ 6.0 _MSC_VER == 1200 span_COMPILER_MSVC_VERSION == 60 (Visual Studio 6.0)
// MSVC++ 7.0 _MSC_VER == 1300 span_COMPILER_MSVC_VERSION == 70 (Visual Studio .NET 2002)
// MSVC++ 7.1 _MSC_VER == 1310 span_COMPILER_MSVC_VERSION == 71 (Visual Studio .NET 2003)
// MSVC++ 8.0 _MSC_VER == 1400 span_COMPILER_MSVC_VERSION == 80 (Visual Studio 2005)
// MSVC++ 9.0 _MSC_VER == 1500 span_COMPILER_MSVC_VERSION == 90 (Visual Studio 2008)
// MSVC++ 10.0 _MSC_VER == 1600 span_COMPILER_MSVC_VERSION == 100 (Visual Studio 2010)
// MSVC++ 11.0 _MSC_VER == 1700 span_COMPILER_MSVC_VERSION == 110 (Visual Studio 2012)
// MSVC++ 12.0 _MSC_VER == 1800 span_COMPILER_MSVC_VERSION == 120 (Visual Studio 2013)
// MSVC++ 14.0 _MSC_VER == 1900 span_COMPILER_MSVC_VERSION == 140 (Visual Studio 2015)
// MSVC++ 14.1 _MSC_VER >= 1910 span_COMPILER_MSVC_VERSION == 141 (Visual Studio 2017)
// MSVC++ 14.2 _MSC_VER >= 1920 span_COMPILER_MSVC_VERSION == 142 (Visual Studio 2019)
#if defined(_MSC_VER ) && !defined(__clang__)
# define span_COMPILER_MSVC_VER (_MSC_VER )
# define span_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) )
#else
# define span_COMPILER_MSVC_VER 0
# define span_COMPILER_MSVC_VERSION 0
#endif
#define span_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) )
#if defined(__clang__)
# define span_COMPILER_CLANG_VERSION span_COMPILER_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
#else
# define span_COMPILER_CLANG_VERSION 0
#endif
#if defined(__GNUC__) && !defined(__clang__)
# define span_COMPILER_GNUC_VERSION span_COMPILER_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
#else
# define span_COMPILER_GNUC_VERSION 0
#endif
// half-open range [lo..hi):
#define span_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) )
// Compiler warning suppression:
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wundef"
# pragma clang diagnostic ignored "-Wmismatched-tags"
# define span_RESTORE_WARNINGS() _Pragma( "clang diagnostic pop" )
#elif defined __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wundef"
# define span_RESTORE_WARNINGS() _Pragma( "GCC diagnostic pop" )
#elif span_COMPILER_MSVC_VER >= 1900
# define span_DISABLE_MSVC_WARNINGS(codes) __pragma(warning(push)) __pragma(warning(disable: codes))
# define span_RESTORE_WARNINGS() __pragma(warning(pop ))
// Suppress the following MSVC GSL warnings:
// - C26439, gsl::f.6 : special function 'function' can be declared 'noexcept'
// - C26440, gsl::f.6 : function 'function' can be declared 'noexcept'
// - C26472, gsl::t.1 : don't use a static_cast for arithmetic conversions;
// use brace initialization, gsl::narrow_cast or gsl::narrow
// - C26473: gsl::t.1 : don't cast between pointer types where the source type and the target type are the same
// - C26481: gsl::b.1 : don't use pointer arithmetic. Use span instead
// - C26490: gsl::t.1 : don't use reinterpret_cast
span_DISABLE_MSVC_WARNINGS( 26439 26440 26472 26473 26481 26490 )
#else
# define span_RESTORE_WARNINGS() /*empty*/
#endif
// Presence of language and library features:
#ifdef _HAS_CPP0X
# define span_HAS_CPP0X _HAS_CPP0X
#else
# define span_HAS_CPP0X 0
#endif
#define span_CPP11_80 (span_CPP11_OR_GREATER || span_COMPILER_MSVC_VER >= 1400)
#define span_CPP11_90 (span_CPP11_OR_GREATER || span_COMPILER_MSVC_VER >= 1500)
#define span_CPP11_100 (span_CPP11_OR_GREATER || span_COMPILER_MSVC_VER >= 1600)
#define span_CPP11_110 (span_CPP11_OR_GREATER || span_COMPILER_MSVC_VER >= 1700)
#define span_CPP11_120 (span_CPP11_OR_GREATER || span_COMPILER_MSVC_VER >= 1800)
#define span_CPP11_140 (span_CPP11_OR_GREATER || span_COMPILER_MSVC_VER >= 1900)
#define span_CPP14_000 (span_CPP14_OR_GREATER)
#define span_CPP14_120 (span_CPP14_OR_GREATER || span_COMPILER_MSVC_VER >= 1800)
#define span_CPP14_140 (span_CPP14_OR_GREATER || span_COMPILER_MSVC_VER >= 1900)
#define span_CPP17_000 (span_CPP17_OR_GREATER)
// Presence of C++11 language features:
#define span_HAVE_ALIAS_TEMPLATE span_CPP11_140
#define span_HAVE_AUTO span_CPP11_100
#define span_HAVE_CONSTEXPR_11 span_CPP11_140
#define span_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG span_CPP11_120
#define span_HAVE_EXPLICIT_CONVERSION span_CPP11_140
#define span_HAVE_INITIALIZER_LIST span_CPP11_120
#define span_HAVE_IS_DEFAULT span_CPP11_140
#define span_HAVE_IS_DELETE span_CPP11_140
#define span_HAVE_NOEXCEPT span_CPP11_140
#define span_HAVE_NORETURN ( span_CPP11_140 && ! span_BETWEEN( span_COMPILER_GNUC_VERSION, 1, 480 ) )
#define span_HAVE_NULLPTR span_CPP11_100
#define span_HAVE_STATIC_ASSERT span_CPP11_100
// Presence of C++14 language features:
#define span_HAVE_CONSTEXPR_14 span_CPP14_000
// Presence of C++17 language features:
#define span_HAVE_DEPRECATED span_CPP17_000
#define span_HAVE_NODISCARD span_CPP17_000
// MSVC: template parameter deduction guides since Visual Studio 2017 v15.7
#if defined(__cpp_deduction_guides)
# define span_HAVE_DEDUCTION_GUIDES 1
#else
# define span_HAVE_DEDUCTION_GUIDES (span_CPP17_OR_GREATER && ! span_BETWEEN( span_COMPILER_MSVC_VER, 1, 1913 ))
#endif
// Presence of C++ library features:
#define span_HAVE_ADDRESSOF span_CPP17_000
#define span_HAVE_ARRAY span_CPP11_110
#define span_HAVE_BYTE span_CPP17_000
#define span_HAVE_CONDITIONAL span_CPP11_120
#define span_HAVE_CONTAINER_DATA_METHOD (span_CPP11_140 || ( span_COMPILER_MSVC_VER >= 1500 && span_HAS_CPP0X ))
#define span_HAVE_DATA span_CPP17_000
#define span_HAVE_LONGLONG span_CPP11_80
#define span_HAVE_REMOVE_CONST span_CPP11_110
#define span_HAVE_SNPRINTF span_CPP11_140
#define span_HAVE_STRUCT_BINDING span_CPP11_120
#define span_HAVE_TYPE_TRAITS span_CPP11_90
// Presence of byte-lite:
#ifdef NONSTD_BYTE_LITE_HPP
# define span_HAVE_NONSTD_BYTE 1
#else
# define span_HAVE_NONSTD_BYTE 0
#endif
// C++ feature usage:
#if span_HAVE_ADDRESSOF
# define span_ADDRESSOF(x) std::addressof(x)
#else
# define span_ADDRESSOF(x) (&x)
#endif
#if span_HAVE_CONSTEXPR_11
# define span_constexpr constexpr
#else
# define span_constexpr /*span_constexpr*/
#endif
#if span_HAVE_CONSTEXPR_14
# define span_constexpr14 constexpr
#else
# define span_constexpr14 /*span_constexpr*/
#endif
#if span_HAVE_EXPLICIT_CONVERSION
# define span_explicit explicit
#else
# define span_explicit /*explicit*/
#endif
#if span_HAVE_IS_DELETE
# define span_is_delete = delete
#else
# define span_is_delete
#endif
#if span_HAVE_IS_DELETE
# define span_is_delete_access public
#else
# define span_is_delete_access private
#endif
#if span_HAVE_NOEXCEPT && ! span_CONFIG_CONTRACT_VIOLATION_THROWS_V
# define span_noexcept noexcept
#else
# define span_noexcept /*noexcept*/
#endif
#if span_HAVE_NULLPTR
# define span_nullptr nullptr
#else
# define span_nullptr NULL
#endif
#if span_HAVE_DEPRECATED
# define span_deprecated(msg) [[deprecated(msg)]]
#else
# define span_deprecated(msg) /*[[deprecated]]*/
#endif
#if span_HAVE_NODISCARD
# define span_nodiscard [[nodiscard]]
#else
# define span_nodiscard /*[[nodiscard]]*/
#endif
#if span_HAVE_NORETURN
# define span_noreturn [[noreturn]]
#else
# define span_noreturn /*[[noreturn]]*/
#endif
// Other features:
#define span_HAVE_CONSTRAINED_SPAN_CONTAINER_CTOR span_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG
#define span_HAVE_ITERATOR_CTOR span_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG
// Additional includes:
#if span_HAVE( ADDRESSOF )
# include <memory>
#endif
#if span_HAVE( ARRAY )
# include <array>
#endif
#if span_HAVE( BYTE )
# include <cstddef>
#endif
#if span_HAVE( DATA )
# include <iterator> // for std::data(), std::size()
#endif
#if span_HAVE( TYPE_TRAITS )
# include <type_traits>
#endif
#if ! span_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR )
# include <vector>
#endif
#if span_FEATURE( MEMBER_AT ) > 1
# include <cstdio>
#endif
#if ! span_CONFIG( NO_EXCEPTIONS )
# include <stdexcept>
#endif
// Contract violation
#define span_ELIDE_CONTRACT_EXPECTS ( 0 == ( span_CONFIG_CONTRACT_LEVEL_MASK & 0x01 ) )
#define span_ELIDE_CONTRACT_ENSURES ( 0 == ( span_CONFIG_CONTRACT_LEVEL_MASK & 0x10 ) )
#if span_ELIDE_CONTRACT_EXPECTS
# define span_constexpr_exp span_constexpr
# define span_EXPECTS( cond ) /* Expect elided */
#else
# define span_constexpr_exp span_constexpr14
# define span_EXPECTS( cond ) span_CONTRACT_CHECK( "Precondition", cond )
#endif
#if span_ELIDE_CONTRACT_ENSURES
# define span_constexpr_ens span_constexpr
# define span_ENSURES( cond ) /* Ensures elided */
#else
# define span_constexpr_ens span_constexpr14
# define span_ENSURES( cond ) span_CONTRACT_CHECK( "Postcondition", cond )
#endif
#define span_CONTRACT_CHECK( type, cond ) \
cond ? static_cast< void >( 0 ) \
: nonstd::span_lite::detail::report_contract_violation( span_LOCATION( __FILE__, __LINE__ ) ": " type " violation." )
#ifdef __GNUG__
# define span_LOCATION( file, line ) file ":" span_STRINGIFY( line )
#else
# define span_LOCATION( file, line ) file "(" span_STRINGIFY( line ) ")"
#endif
// Method enabling
#if span_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG )
#define span_REQUIRES_0(VA) \
template< bool B = (VA), typename std::enable_if<B, int>::type = 0 >
# if span_BETWEEN( span_COMPILER_MSVC_VERSION, 1, 140 )
// VS 2013 and earlier seem to have trouble with SFINAE for default non-type arguments
# define span_REQUIRES_T(VA) \
, typename = typename std::enable_if< ( VA ), nonstd::span_lite::detail::enabler >::type
# else
# define span_REQUIRES_T(VA) \
, typename std::enable_if< (VA), int >::type = 0
# endif
#define span_REQUIRES_R(R, VA) \
typename std::enable_if< (VA), R>::type
#define span_REQUIRES_A(VA) \
, typename std::enable_if< (VA), void*>::type = nullptr
#else
# define span_REQUIRES_0(VA) /*empty*/
# define span_REQUIRES_T(VA) /*empty*/
# define span_REQUIRES_R(R, VA) R
# define span_REQUIRES_A(VA) /*empty*/
#endif
namespace nonstd {
namespace span_lite {
// [views.constants], constants
typedef span_CONFIG_EXTENT_TYPE extent_t;
typedef span_CONFIG_SIZE_TYPE size_t;
span_constexpr const extent_t dynamic_extent = static_cast<extent_t>( -1 );
template< class T, extent_t Extent = dynamic_extent >
class span;
// Tag to select span constructor taking a container (prevent ms-gsl warning C26426):
struct with_container_t { span_constexpr with_container_t() span_noexcept {} };
const span_constexpr with_container_t with_container;
// C++11 emulation:
namespace std11 {
#if span_HAVE( REMOVE_CONST )
using std::remove_cv;
using std::remove_const;
using std::remove_volatile;
#else
template< class T > struct remove_const { typedef T type; };
template< class T > struct remove_const< T const > { typedef T type; };
template< class T > struct remove_volatile { typedef T type; };
template< class T > struct remove_volatile< T volatile > { typedef T type; };
template< class T >
struct remove_cv
{
typedef typename std11::remove_volatile< typename std11::remove_const< T >::type >::type type;
};
#endif // span_HAVE( REMOVE_CONST )
#if span_HAVE( TYPE_TRAITS )
using std::is_same;
using std::is_signed;
using std::integral_constant;
using std::true_type;
using std::false_type;
using std::remove_reference;
#else
template< class T, T v > struct integral_constant { enum { value = v }; };
typedef integral_constant< bool, true > true_type;
typedef integral_constant< bool, false > false_type;
template< class T, class U > struct is_same : false_type{};
template< class T > struct is_same<T, T> : true_type{};
template< typename T > struct is_signed : false_type {};
template<> struct is_signed<signed char> : true_type {};
template<> struct is_signed<signed int > : true_type {};
template<> struct is_signed<signed long> : true_type {};
#endif
} // namespace std11
// C++17 emulation:
namespace std17 {
template< bool v > struct bool_constant : std11::integral_constant<bool, v>{};
#if span_CPP11_120
template< class...>
using void_t = void;
#endif
#if span_HAVE( DATA )
using std::data;
using std::size;
#elif span_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR )
template< typename T, std::size_t N >
inline span_constexpr auto size( const T(&)[N] ) span_noexcept -> size_t
{
return N;
}
template< typename C >
inline span_constexpr auto size( C const & cont ) -> decltype( cont.size() )
{
return cont.size();
}
template< typename T, std::size_t N >
inline span_constexpr auto data( T(&arr)[N] ) span_noexcept -> T*
{
return &arr[0];
}
template< typename C >
inline span_constexpr auto data( C & cont ) -> decltype( cont.data() )
{
return cont.data();
}
template< typename C >
inline span_constexpr auto data( C const & cont ) -> decltype( cont.data() )
{
return cont.data();
}
template< typename E >
inline span_constexpr auto data( std::initializer_list<E> il ) span_noexcept -> E const *
{
return il.begin();
}
#endif // span_HAVE( DATA )
#if span_HAVE( BYTE )
using std::byte;
#elif span_HAVE( NONSTD_BYTE )
using nonstd::byte;
#endif
} // namespace std17
// C++20 emulation:
namespace std20 {
#if span_HAVE( DEDUCTION_GUIDES )
template< class T >
using iter_reference_t = decltype( *std::declval<T&>() );
#endif
} // namespace std20
// Implementation details:
namespace detail {
/*enum*/ struct enabler{};
template< typename T >
span_constexpr bool is_positive( T x )
{
return std11::is_signed<T>::value ? x >= 0 : true;
}
#if span_HAVE( TYPE_TRAITS )
template< class Q >
struct is_span_oracle : std::false_type{};
template< class T, span_CONFIG_EXTENT_TYPE Extent >
struct is_span_oracle< span<T, Extent> > : std::true_type{};
template< class Q >
struct is_span : is_span_oracle< typename std::remove_cv<Q>::type >{};
template< class Q >
struct is_std_array_oracle : std::false_type{};
#if span_HAVE( ARRAY )
template< class T, std::size_t Extent >
struct is_std_array_oracle< std::array<T, Extent> > : std::true_type{};
#endif
template< class Q >
struct is_std_array : is_std_array_oracle< typename std::remove_cv<Q>::type >{};
template< class Q >
struct is_array : std::false_type {};
template< class T >
struct is_array<T[]> : std::true_type {};
template< class T, std::size_t N >
struct is_array<T[N]> : std::true_type {};
#if span_CPP11_140 && ! span_BETWEEN( span_COMPILER_GNUC_VERSION, 1, 500 )
template< class, class = void >
struct has_size_and_data : std::false_type{};
template< class C >
struct has_size_and_data
<
C, std17::void_t<
decltype( std17::size(std::declval<C>()) ),
decltype( std17::data(std::declval<C>()) ) >
> : std::true_type{};
template< class, class, class = void >
struct is_compatible_element : std::false_type {};
template< class C, class E >
struct is_compatible_element
<
C, E, std17::void_t<
decltype( std17::data(std::declval<C>()) ) >
> : std::is_convertible< typename std::remove_pointer<decltype( std17::data( std::declval<C&>() ) )>::type(*)[], E(*)[] >{};
template< class C >
struct is_container : std17::bool_constant
<
! is_span< C >::value
&& ! is_array< C >::value
&& ! is_std_array< C >::value
&& has_size_and_data< C >::value
>{};
template< class C, class E >
struct is_compatible_container : std17::bool_constant
<
is_container<C>::value
&& is_compatible_element<C,E>::value
>{};
#else // span_CPP11_140
template<
class C, class E
span_REQUIRES_T((
! is_span< C >::value
&& ! is_array< C >::value
&& ! is_std_array< C >::value
&& ( std::is_convertible< typename std::remove_pointer<decltype( std17::data( std::declval<C&>() ) )>::type(*)[], E(*)[] >::value)
// && has_size_and_data< C >::value
))
, class = decltype( std17::size(std::declval<C>()) )
, class = decltype( std17::data(std::declval<C>()) )
>
struct is_compatible_container : std::true_type{};
#endif // span_CPP11_140
#endif // span_HAVE( TYPE_TRAITS )
#if ! span_CONFIG( NO_EXCEPTIONS )
#if span_FEATURE( MEMBER_AT ) > 1
// format index and size:
#if defined(__clang__)
# pragma clang diagnostic ignored "-Wlong-long"
#elif defined __GNUC__
# pragma GCC diagnostic ignored "-Wformat=ll"
# pragma GCC diagnostic ignored "-Wlong-long"
#endif
span_noreturn inline void throw_out_of_range( size_t idx, size_t size )
{
const char fmt[] = "span::at(): index '%lli' is out of range [0..%lli)";
char buffer[ 2 * 20 + sizeof fmt ];
sprintf( buffer, fmt, static_cast<long long>(idx), static_cast<long long>(size) );
throw std::out_of_range( buffer );
}
#else // MEMBER_AT
span_noreturn inline void throw_out_of_range( size_t /*idx*/, size_t /*size*/ )
{
throw std::out_of_range( "span::at(): index outside span" );
}
#endif // MEMBER_AT
#endif // NO_EXCEPTIONS
#if span_CONFIG( CONTRACT_VIOLATION_THROWS_V )
struct contract_violation : std::logic_error
{
explicit contract_violation( char const * const message )
: std::logic_error( message )
{}
};
inline void report_contract_violation( char const * msg )
{
throw contract_violation( msg );
}
#else // span_CONFIG( CONTRACT_VIOLATION_THROWS_V )
span_noreturn inline void report_contract_violation( char const * /*msg*/ ) span_noexcept
{
std::terminate();
}
#endif // span_CONFIG( CONTRACT_VIOLATION_THROWS_V )
} // namespace detail
// Prevent signed-unsigned mismatch:
#define span_sizeof(T) static_cast<extent_t>( sizeof(T) )
template< class T >
inline span_constexpr size_t to_size( T size )
{
return static_cast<size_t>( size );
}
//
// [views.span] - A view over a contiguous, single-dimension sequence of objects
//
template< class T, extent_t Extent /*= dynamic_extent*/ >
class span
{
public:
// constants and types
typedef T element_type;
typedef typename std11::remove_cv< T >::type value_type;
typedef T & reference;
typedef T * pointer;
typedef T const * const_pointer;
typedef T const & const_reference;
typedef size_t size_type;
typedef extent_t extent_type;
typedef pointer iterator;
typedef const_pointer const_iterator;
typedef std::ptrdiff_t difference_type;
typedef std::reverse_iterator< iterator > reverse_iterator;
typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
// static constexpr extent_type extent = Extent;
enum { extent = Extent };
// 26.7.3.2 Constructors, copy, and assignment [span.cons]
span_REQUIRES_0(
( Extent == 0 ) ||
( Extent == dynamic_extent )
)
span_constexpr span() span_noexcept
: data_( span_nullptr )
, size_( 0 )
{
// span_EXPECTS( data() == span_nullptr );
// span_EXPECTS( size() == 0 );
}
#if span_HAVE( ITERATOR_CTOR )
// Didn't yet succeed in combining the next two constructors:
span_constexpr_exp span( std::nullptr_t, size_type count )
: data_( span_nullptr )
, size_( count )
{
span_EXPECTS( data_ == span_nullptr && count == 0 );
}
template< typename It
span_REQUIRES_T((
std::is_convertible<decltype(*std::declval<It&>()), element_type &>::value
))
>
span_constexpr_exp span( It first, size_type count )
: data_( to_address( first ) )
, size_( count )
{
span_EXPECTS(
( data_ == span_nullptr && count == 0 ) ||
( data_ != span_nullptr && detail::is_positive( count ) )
);
}
#else
span_constexpr_exp span( pointer ptr, size_type count )
: data_( ptr )
, size_( count )
{
span_EXPECTS(
( ptr == span_nullptr && count == 0 ) ||
( ptr != span_nullptr && detail::is_positive( count ) )
);
}
#endif
#if span_HAVE( ITERATOR_CTOR )
template< typename It, typename End
span_REQUIRES_T((
std::is_convertible<decltype(&*std::declval<It&>()), element_type *>::value
&& ! std::is_convertible<End, std::size_t>::value
))
>
span_constexpr_exp span( It first, End last )
: data_( to_address( first ) )
, size_( to_size( last - first ) )
{
span_EXPECTS(
last - first >= 0
);
}
#else
span_constexpr_exp span( pointer first, pointer last )
: data_( first )
, size_( to_size( last - first ) )
{
span_EXPECTS(
last - first >= 0
);
}
#endif
template< std::size_t N
span_REQUIRES_T((
(Extent == dynamic_extent || Extent == static_cast<extent_t>(N))
&& std::is_convertible< value_type(*)[], element_type(*)[] >::value
))
>
span_constexpr span( element_type ( &arr )[ N ] ) span_noexcept
: data_( span_ADDRESSOF( arr[0] ) )
, size_( N )
{}
#if span_HAVE( ARRAY )
template< std::size_t N
span_REQUIRES_T((
(Extent == dynamic_extent || Extent == static_cast<extent_t>(N))