/
functional
2931 lines (2426 loc) · 114 KB
/
functional
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
// functional standard header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#pragma once
#ifndef _FUNCTIONAL_
#define _FUNCTIONAL_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <exception>
#include <tuple>
#include <typeinfo>
#include <xmemory>
#if _HAS_CXX17
#ifdef _LEGACY_CODE_ASSUMES_FUNCTIONAL_INCLUDES_MEMORY
#include <memory>
#endif // _LEGACY_CODE_ASSUMES_FUNCTIONAL_INCLUDES_MEMORY
#include <unordered_map>
#endif // _HAS_CXX17
#ifdef __cpp_lib_concepts
#include <compare>
#endif // __cpp_lib_concepts
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
// plus, minus, and multiplies are defined in <xutility>
_EXPORT_STD template <class _Ty = void>
struct divides {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
_NODISCARD constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left / _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct modulus {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
_NODISCARD constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left % _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct negate {
using _ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
_NODISCARD constexpr _Ty operator()(const _Ty& _Left) const {
return -_Left;
}
};
// less is defined in <type_traits>
// equal_to, not_equal_to, greater, greater_equal, and less_equal are defined in <xutility>
_EXPORT_STD template <class _Ty = void>
struct logical_and {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = bool;
_NODISCARD constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left && _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct logical_or {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = bool;
_NODISCARD constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left || _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct logical_not {
using _ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = bool;
_NODISCARD constexpr bool operator()(const _Ty& _Left) const {
return !_Left;
}
};
_EXPORT_STD template <class _Ty = void>
struct bit_and {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
_NODISCARD constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left & _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct bit_or {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
_NODISCARD constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left | _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct bit_xor {
using _FIRST_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _SECOND_ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
_NODISCARD constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left ^ _Right;
}
};
_EXPORT_STD template <class _Ty = void>
struct bit_not {
using _ARGUMENT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
using _RESULT_TYPE_NAME _CXX17_DEPRECATE_ADAPTOR_TYPEDEFS = _Ty;
_NODISCARD constexpr _Ty operator()(const _Ty& _Left) const {
return ~_Left;
}
};
// void specializations of plus, minus, and multiplies are defined in <xutility>
template <>
struct divides<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
-> decltype(_STD forward<_Ty1>(_Left) / _STD forward<_Ty2>(_Right)) {
return _STD forward<_Ty1>(_Left) / _STD forward<_Ty2>(_Right);
}
using is_transparent = int;
};
template <>
struct modulus<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
-> decltype(_STD forward<_Ty1>(_Left) % _STD forward<_Ty2>(_Right)) {
return _STD forward<_Ty1>(_Left) % _STD forward<_Ty2>(_Right);
}
using is_transparent = int;
};
template <>
struct negate<void> {
template <class _Ty>
_NODISCARD constexpr auto operator()(_Ty&& _Left) const -> decltype(-_STD forward<_Ty>(_Left)) {
return -_STD forward<_Ty>(_Left);
}
using is_transparent = int;
};
// void specialization of less is defined in <type_traits>
// void specializations of equal_to, not_equal_to, greater, greater_equal, and less_equal are defined in <xutility>
template <>
struct logical_and<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
-> decltype(_STD forward<_Ty1>(_Left) && _STD forward<_Ty2>(_Right)) {
return _STD forward<_Ty1>(_Left) && _STD forward<_Ty2>(_Right);
}
using is_transparent = int;
};
template <>
struct logical_or<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
-> decltype(_STD forward<_Ty1>(_Left) || _STD forward<_Ty2>(_Right)) {
return _STD forward<_Ty1>(_Left) || _STD forward<_Ty2>(_Right);
}
using is_transparent = int;
};
template <>
struct logical_not<void> {
template <class _Ty>
_NODISCARD constexpr auto operator()(_Ty&& _Left) const -> decltype(!_STD forward<_Ty>(_Left)) {
return !_STD forward<_Ty>(_Left);
}
using is_transparent = int;
};
template <>
struct bit_and<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
-> decltype(_STD forward<_Ty1>(_Left) & _STD forward<_Ty2>(_Right)) {
return _STD forward<_Ty1>(_Left) & _STD forward<_Ty2>(_Right);
}
using is_transparent = int;
};
template <>
struct bit_or<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
-> decltype(_STD forward<_Ty1>(_Left) | _STD forward<_Ty2>(_Right)) {
return _STD forward<_Ty1>(_Left) | _STD forward<_Ty2>(_Right);
}
using is_transparent = int;
};
template <>
struct bit_xor<void> {
template <class _Ty1, class _Ty2>
_NODISCARD constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const
-> decltype(_STD forward<_Ty1>(_Left) ^ _STD forward<_Ty2>(_Right)) {
return _STD forward<_Ty1>(_Left) ^ _STD forward<_Ty2>(_Right);
}
using is_transparent = int;
};
template <>
struct bit_not<void> {
template <class _Ty>
_NODISCARD constexpr auto operator()(_Ty&& _Left) const -> decltype(~_STD forward<_Ty>(_Left)) {
return ~_STD forward<_Ty>(_Left);
}
using is_transparent = int;
};
#if _HAS_DEPRECATED_NEGATORS
_STL_DISABLE_DEPRECATED_WARNING
_EXPORT_STD template <class _Fn>
class _CXX17_DEPRECATE_NEGATORS unary_negate {
public:
using argument_type = typename _Fn::argument_type;
using result_type = bool;
constexpr explicit unary_negate(const _Fn& _Func) : _Functor(_Func) {}
_NODISCARD constexpr bool operator()(const argument_type& _Left) const {
return !_Functor(_Left);
}
private:
_Fn _Functor;
};
_EXPORT_STD template <class _Fn>
_CXX17_DEPRECATE_NEGATORS _NODISCARD constexpr unary_negate<_Fn> not1(const _Fn& _Func) {
return unary_negate<_Fn>(_Func);
}
_EXPORT_STD template <class _Fn>
class _CXX17_DEPRECATE_NEGATORS binary_negate {
public:
using first_argument_type = typename _Fn::first_argument_type;
using second_argument_type = typename _Fn::second_argument_type;
using result_type = bool;
constexpr explicit binary_negate(const _Fn& _Func) : _Functor(_Func) {}
_NODISCARD constexpr bool operator()(const first_argument_type& _Left, const second_argument_type& _Right) const {
return !_Functor(_Left, _Right);
}
private:
_Fn _Functor;
};
_EXPORT_STD template <class _Fn>
_CXX17_DEPRECATE_NEGATORS _NODISCARD constexpr binary_negate<_Fn> not2(const _Fn& _Func) {
return binary_negate<_Fn>(_Func);
}
_STL_RESTORE_DEPRECATED_WARNING
#endif // _HAS_DEPRECATED_NEGATORS
#if _HAS_AUTO_PTR_ETC
_EXPORT_STD template <class _Arg, class _Result>
struct unary_function { // base class for unary functions
using argument_type = _Arg;
using result_type = _Result;
};
_EXPORT_STD template <class _Arg1, class _Arg2, class _Result>
struct binary_function { // base class for binary functions
using first_argument_type = _Arg1;
using second_argument_type = _Arg2;
using result_type = _Result;
};
_STL_DISABLE_DEPRECATED_WARNING
_EXPORT_STD template <class _Fn>
class binder1st : public unary_function<typename _Fn::second_argument_type,
typename _Fn::result_type> { // functor adapter _Func(stored, right)
public:
using _Base = unary_function<typename _Fn::second_argument_type, typename _Fn::result_type>;
using argument_type = typename _Base::argument_type;
using result_type = typename _Base::result_type;
binder1st(const _Fn& _Func, const typename _Fn::first_argument_type& _Left) : op(_Func), value(_Left) {}
result_type operator()(const argument_type& _Right) const {
return op(value, _Right);
}
result_type operator()(argument_type& _Right) const {
return op(value, _Right);
}
protected:
_Fn op;
typename _Fn::first_argument_type value; // the left operand
};
_EXPORT_STD template <class _Fn, class _Ty>
_NODISCARD binder1st<_Fn> bind1st(const _Fn& _Func, const _Ty& _Left) {
typename _Fn::first_argument_type _Val(_Left);
return binder1st<_Fn>(_Func, _Val);
}
_EXPORT_STD template <class _Fn>
class binder2nd : public unary_function<typename _Fn::first_argument_type,
typename _Fn::result_type> { // functor adapter _Func(left, stored)
public:
using _Base = unary_function<typename _Fn::first_argument_type, typename _Fn::result_type>;
using argument_type = typename _Base::argument_type;
using result_type = typename _Base::result_type;
binder2nd(const _Fn& _Func, const typename _Fn::second_argument_type& _Right) : op(_Func), value(_Right) {}
result_type operator()(const argument_type& _Left) const {
return op(_Left, value);
}
result_type operator()(argument_type& _Left) const {
return op(_Left, value);
}
protected:
_Fn op;
typename _Fn::second_argument_type value; // the right operand
};
_EXPORT_STD template <class _Fn, class _Ty>
_NODISCARD binder2nd<_Fn> bind2nd(const _Fn& _Func, const _Ty& _Right) {
typename _Fn::second_argument_type _Val(_Right);
return binder2nd<_Fn>(_Func, _Val);
}
_STL_RESTORE_DEPRECATED_WARNING
_EXPORT_STD template <class _Arg, class _Result, class _Fn = _Result(*)(_Arg)>
class pointer_to_unary_function : public unary_function<_Arg, _Result> { // functor adapter (*pfunc)(left)
public:
explicit pointer_to_unary_function(_Fn _Left) : _Pfun(_Left) {}
_Result operator()(_Arg _Left) const {
return _Pfun(_Left);
}
protected:
_Fn _Pfun; // the function pointer
};
_EXPORT_STD template <class _Arg1, class _Arg2, class _Result, class _Fn = _Result(*)(_Arg1, _Arg2)>
class pointer_to_binary_function
: public binary_function<_Arg1, _Arg2, _Result> { // functor adapter (*pfunc)(left, right)
public:
explicit pointer_to_binary_function(_Fn _Left) : _Pfun(_Left) {}
_Result operator()(_Arg1 _Left, _Arg2 _Right) const {
return _Pfun(_Left, _Right);
}
protected:
_Fn _Pfun; // the function pointer
};
#define _PTR_FUN(CALL_OPT, X1, X2, X3) \
_EXPORT_STD template <class _Arg, class _Result> \
_NODISCARD pointer_to_unary_function<_Arg, _Result, _Result(CALL_OPT*)(_Arg)> ptr_fun( \
_Result(CALL_OPT* _Left)(_Arg)) { \
return pointer_to_unary_function<_Arg, _Result, _Result(CALL_OPT*)(_Arg)>(_Left); \
} \
_EXPORT_STD template <class _Arg1, class _Arg2, class _Result> \
_NODISCARD pointer_to_binary_function<_Arg1, _Arg2, _Result, _Result(CALL_OPT*)(_Arg1, _Arg2)> ptr_fun( \
_Result(CALL_OPT* _Left)(_Arg1, _Arg2)) { \
return pointer_to_binary_function<_Arg1, _Arg2, _Result, _Result(CALL_OPT*)(_Arg1, _Arg2)>(_Left); \
}
_NON_MEMBER_CALL(_PTR_FUN, X1, X2, X3)
#undef _PTR_FUN
_EXPORT_STD template <class _Result, class _Ty>
class mem_fun_t : public unary_function<_Ty*, _Result> { // functor adapter (*p->*pfunc)(), non-const *pfunc
public:
explicit mem_fun_t(_Result (_Ty::*_Pm)()) : _Pmemfun(_Pm) {}
_Result operator()(_Ty* _Pleft) const {
return (_Pleft->*_Pmemfun)();
}
private:
_Result (_Ty::*_Pmemfun)(); // the member function pointer
};
_EXPORT_STD template <class _Result, class _Ty, class _Arg>
class mem_fun1_t : public binary_function<_Ty*, _Arg, _Result> { // functor adapter (*p->*pfunc)(val), non-const *pfunc
public:
explicit mem_fun1_t(_Result (_Ty::*_Pm)(_Arg)) : _Pmemfun(_Pm) {}
_Result operator()(_Ty* _Pleft, _Arg _Right) const {
return (_Pleft->*_Pmemfun)(_Right);
}
private:
_Result (_Ty::*_Pmemfun)(_Arg); // the member function pointer
};
_EXPORT_STD template <class _Result, class _Ty>
class const_mem_fun_t : public unary_function<const _Ty*, _Result> { // functor adapter (*p->*pfunc)(), const *pfunc
public:
explicit const_mem_fun_t(_Result (_Ty::*_Pm)() const) : _Pmemfun(_Pm) {}
_Result operator()(const _Ty* _Pleft) const {
return (_Pleft->*_Pmemfun)();
}
private:
_Result (_Ty::*_Pmemfun)() const; // the member function pointer
};
_EXPORT_STD template <class _Result, class _Ty, class _Arg>
class const_mem_fun1_t
: public binary_function<const _Ty*, _Arg, _Result> { // functor adapter (*p->*pfunc)(val), const *pfunc
public:
explicit const_mem_fun1_t(_Result (_Ty::*_Pm)(_Arg) const) : _Pmemfun(_Pm) {}
_Result operator()(const _Ty* _Pleft, _Arg _Right) const {
return (_Pleft->*_Pmemfun)(_Right);
}
private:
_Result (_Ty::*_Pmemfun)(_Arg) const; // the member function pointer
};
_EXPORT_STD template <class _Result, class _Ty>
_NODISCARD mem_fun_t<_Result, _Ty> mem_fun(_Result (_Ty::*_Pm)()) {
return mem_fun_t<_Result, _Ty>(_Pm);
}
_EXPORT_STD template <class _Result, class _Ty, class _Arg>
_NODISCARD mem_fun1_t<_Result, _Ty, _Arg> mem_fun(_Result (_Ty::*_Pm)(_Arg)) {
return mem_fun1_t<_Result, _Ty, _Arg>(_Pm);
}
_EXPORT_STD template <class _Result, class _Ty>
_NODISCARD const_mem_fun_t<_Result, _Ty> mem_fun(_Result (_Ty::*_Pm)() const) {
return const_mem_fun_t<_Result, _Ty>(_Pm);
}
_EXPORT_STD template <class _Result, class _Ty, class _Arg>
_NODISCARD const_mem_fun1_t<_Result, _Ty, _Arg> mem_fun(_Result (_Ty::*_Pm)(_Arg) const) {
return const_mem_fun1_t<_Result, _Ty, _Arg>(_Pm);
}
_EXPORT_STD template <class _Result, class _Ty>
class mem_fun_ref_t : public unary_function<_Ty, _Result> { // functor adapter (*left.*pfunc)(), non-const *pfunc
public:
explicit mem_fun_ref_t(_Result (_Ty::*_Pm)()) : _Pmemfun(_Pm) {}
_Result operator()(_Ty& _Left) const {
return (_Left.*_Pmemfun)();
}
private:
_Result (_Ty::*_Pmemfun)(); // the member function pointer
};
_EXPORT_STD template <class _Result, class _Ty, class _Arg>
class mem_fun1_ref_t
: public binary_function<_Ty, _Arg, _Result> { // functor adapter (*left.*pfunc)(val), non-const *pfunc
public:
explicit mem_fun1_ref_t(_Result (_Ty::*_Pm)(_Arg)) : _Pmemfun(_Pm) {}
_Result operator()(_Ty& _Left, _Arg _Right) const {
return (_Left.*_Pmemfun)(_Right);
}
private:
_Result (_Ty::*_Pmemfun)(_Arg); // the member function pointer
};
_EXPORT_STD template <class _Result, class _Ty>
class const_mem_fun_ref_t : public unary_function<_Ty, _Result> { // functor adapter (*left.*pfunc)(), const *pfunc
public:
explicit const_mem_fun_ref_t(_Result (_Ty::*_Pm)() const) : _Pmemfun(_Pm) {}
_Result operator()(const _Ty& _Left) const {
return (_Left.*_Pmemfun)();
}
private:
_Result (_Ty::*_Pmemfun)() const; // the member function pointer
};
_EXPORT_STD template <class _Result, class _Ty, class _Arg>
class const_mem_fun1_ref_t
: public binary_function<_Ty, _Arg, _Result> { // functor adapter (*left.*pfunc)(val), const *pfunc
public:
explicit const_mem_fun1_ref_t(_Result (_Ty::*_Pm)(_Arg) const) : _Pmemfun(_Pm) {}
_Result operator()(const _Ty& _Left, _Arg _Right) const {
return (_Left.*_Pmemfun)(_Right);
}
private:
_Result (_Ty::*_Pmemfun)(_Arg) const; // the member function pointer
};
_EXPORT_STD template <class _Result, class _Ty>
_NODISCARD mem_fun_ref_t<_Result, _Ty> mem_fun_ref(_Result (_Ty::*_Pm)()) {
return mem_fun_ref_t<_Result, _Ty>(_Pm);
}
_EXPORT_STD template <class _Result, class _Ty, class _Arg>
_NODISCARD mem_fun1_ref_t<_Result, _Ty, _Arg> mem_fun_ref(_Result (_Ty::*_Pm)(_Arg)) {
return mem_fun1_ref_t<_Result, _Ty, _Arg>(_Pm);
}
_EXPORT_STD template <class _Result, class _Ty>
_NODISCARD const_mem_fun_ref_t<_Result, _Ty> mem_fun_ref(_Result (_Ty::*_Pm)() const) {
return const_mem_fun_ref_t<_Result, _Ty>(_Pm);
}
_EXPORT_STD template <class _Result, class _Ty, class _Arg>
_NODISCARD const_mem_fun1_ref_t<_Result, _Ty, _Arg> mem_fun_ref(_Result (_Ty::*_Pm)(_Arg) const) {
return const_mem_fun1_ref_t<_Result, _Ty, _Arg>(_Pm);
}
#endif // _HAS_AUTO_PTR_ETC
template <class _Memptr>
class _Mem_fn : public _Weak_types<_Memptr> {
private:
_Memptr _Pm;
public:
constexpr explicit _Mem_fn(_Memptr _Val) noexcept : _Pm(_Val) {}
template <class... _Types>
_CONSTEXPR20 auto operator()(_Types&&... _Args) const
noexcept(noexcept(_STD invoke(_Pm, _STD forward<_Types>(_Args)...)))
-> decltype(_STD invoke(_Pm, _STD forward<_Types>(_Args)...)) {
return _STD invoke(_Pm, _STD forward<_Types>(_Args)...);
}
};
_EXPORT_STD template <class _Rx, class _Ty>
_NODISCARD _CONSTEXPR20 _Mem_fn<_Rx _Ty::*> mem_fn(_Rx _Ty::*_Pm) noexcept {
return _Mem_fn<_Rx _Ty::*>(_Pm);
}
#if _HAS_CXX17
struct _Not_fn_tag {
explicit _Not_fn_tag() = default;
};
template <class _Decayed>
class _Not_fn : private _Ebco_base<_Decayed> {
private:
using _Mybase = _Ebco_base<_Decayed>;
public:
template <class _Callable, class _Tag, enable_if_t<is_same_v<_Tag, _Not_fn_tag>, int> = 0>
constexpr explicit _Not_fn(_Callable&& _Obj, _Tag) noexcept(
is_nothrow_constructible_v<_Decayed, _Callable>) // strengthened
: _Mybase(_STD forward<_Callable>(_Obj)) {} // store a callable object
constexpr _Not_fn(const _Not_fn&) = default;
constexpr _Not_fn(_Not_fn&&) = default;
template <class... _Types>
_CONSTEXPR20 auto operator()(_Types&&... _Args) & noexcept(
noexcept(!_STD invoke(this->_Get_val(), _STD forward<_Types>(_Args)...)))
-> decltype(!_STD declval<invoke_result_t<_Decayed&, _Types...>>()) {
return !_STD invoke(this->_Get_val(), _STD forward<_Types>(_Args)...);
}
template <class... _Types>
_CONSTEXPR20 auto operator()(_Types&&... _Args) const& noexcept(
noexcept(!_STD invoke(this->_Get_val(), _STD forward<_Types>(_Args)...)))
-> decltype(!_STD declval<invoke_result_t<const _Decayed&, _Types...>>()) {
return !_STD invoke(this->_Get_val(), _STD forward<_Types>(_Args)...);
}
template <class... _Types>
_CONSTEXPR20 auto operator()(_Types&&... _Args) && noexcept(
noexcept(!_STD invoke(_STD move(this->_Get_val()), _STD forward<_Types>(_Args)...)))
-> decltype(!_STD declval<invoke_result_t<_Decayed, _Types...>>()) {
return !_STD invoke(_STD move(this->_Get_val()), _STD forward<_Types>(_Args)...);
}
template <class... _Types>
_CONSTEXPR20 auto operator()(_Types&&... _Args) const&& noexcept(
noexcept(!_STD invoke(_STD move(this->_Get_val()), _STD forward<_Types>(_Args)...)))
-> decltype(!_STD declval<invoke_result_t<const _Decayed, _Types...>>()) {
return !_STD invoke(_STD move(this->_Get_val()), _STD forward<_Types>(_Args)...);
}
};
_EXPORT_STD template <class _Callable>
_NODISCARD _CONSTEXPR20 _Not_fn<decay_t<_Callable>> not_fn(_Callable&& _Obj) noexcept(
is_nothrow_constructible_v<decay_t<_Callable>, _Callable>) /* strengthened */ {
// wrap a callable object to be negated
return _Not_fn<decay_t<_Callable>>(_STD forward<_Callable>(_Obj), _Not_fn_tag{});
}
#endif // _HAS_CXX17
_EXPORT_STD class bad_function_call : public exception { // exception thrown when an empty std::function is called
public:
bad_function_call() noexcept {}
_NODISCARD const char* __CLR_OR_THIS_CALL what() const noexcept override {
// return pointer to message string
return "bad function call";
}
};
extern "C++" [[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Xbad_function_call();
struct _Unforced { // tag to distinguish bind() from bind<R>()
explicit _Unforced() = default;
};
// helper to give INVOKE an explicit return type; avoids undesirable Expression SFINAE
template <class _Rx>
struct _Invoker_ret { // selected for all _Rx other than _Unforced
template <class _Fx, class... _Valtys,
enable_if_t<_Select_invoke_traits<_Fx, _Valtys...>::template _Is_invocable_r<_Rx>::value, int> = 0>
static _CONSTEXPR20 _Rx _Call(_Fx&& _Func, _Valtys&&... _Vals) noexcept(_Select_invoke_traits<_Fx,
_Valtys...>::template _Is_nothrow_invocable_r<_Rx>::value) { // INVOKE, implicitly converted
if constexpr (is_void_v<_Rx>) {
_STL_INTERNAL_STATIC_ASSERT(
_Select_invoke_traits<_Fx, _Valtys...>::_Is_nothrow_invocable::value
== _Select_invoke_traits<_Fx, _Valtys...>::template _Is_nothrow_invocable_r<_Rx>::value);
_STD invoke(static_cast<_Fx&&>(_Func), static_cast<_Valtys&&>(_Vals)...);
} else {
return _STD invoke(static_cast<_Fx&&>(_Func), static_cast<_Valtys&&>(_Vals)...);
}
}
};
template <>
struct _Invoker_ret<_Unforced> { // selected for _Rx being _Unforced
template <class _Fx, class... _Valtys>
static _CONSTEXPR20 auto _Call(_Fx&& _Func, _Valtys&&... _Vals) noexcept(
_Select_invoke_traits<_Fx, _Valtys...>::_Is_nothrow_invocable::value)
-> decltype(_STD invoke(static_cast<_Fx&&>(_Func), static_cast<_Valtys&&>(_Vals)...)) { // INVOKE, unchanged
return _STD invoke(static_cast<_Fx&&>(_Func), static_cast<_Valtys&&>(_Vals)...);
}
};
_EXPORT_STD template <class _Fty>
class function;
template <class _Ty>
_INLINE_VAR constexpr bool _Testable_callable_v =
disjunction_v<is_pointer<_Ty>, _Is_specialization<_Ty, function>, is_member_pointer<_Ty>>;
template <class _Ty>
bool _Test_callable(const _Ty& _Arg) noexcept { // determine whether std::function must store _Arg
if constexpr (_Testable_callable_v<_Ty>) {
return !!_Arg;
} else {
return true;
}
}
template <class _Rx, class... _Types>
class __declspec(novtable) _Func_base { // abstract base for implementation types
public:
virtual _Func_base* _Copy(void*) const = 0;
virtual _Func_base* _Move(void*) noexcept = 0;
virtual _Rx _Do_call(_Types&&...) = 0;
virtual const type_info& _Target_type() const noexcept = 0;
virtual void _Delete_this(bool) noexcept = 0;
#if _HAS_STATIC_RTTI
const void* _Target(const type_info& _Info) const noexcept {
return _Target_type() == _Info ? _Get() : nullptr;
}
#endif // _HAS_STATIC_RTTI
_Func_base() = default;
_Func_base(const _Func_base&) = delete;
_Func_base& operator=(const _Func_base&) = delete;
// dtor non-virtual due to _Delete_this()
private:
virtual const void* _Get() const noexcept = 0;
};
_INLINE_VAR constexpr size_t _Space_size = (_Small_object_num_ptrs - 1) * sizeof(void*);
template <class _Impl> // determine whether _Impl must be dynamically allocated
_INLINE_VAR constexpr bool _Is_large = sizeof(_Impl) > _Space_size || alignof(_Impl) > alignof(max_align_t)
|| !_Impl::_Nothrow_move::value;
#if _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Callable, class _Alloc, class _Rx, class... _Types>
class _Func_impl final : public _Func_base<_Rx, _Types...> {
// derived class for specific implementation types that use allocators
public:
using _Mybase = _Func_base<_Rx, _Types...>;
using _Myalty = _Rebind_alloc_t<_Alloc, _Func_impl>;
using _Myalty_traits = allocator_traits<_Myalty>;
using _Nothrow_move = is_nothrow_move_constructible<_Callable>;
template <class _Other1, class _Other2>
_Func_impl(_Other1&& _Val, _Other2&& _Ax)
: _Mypair(_One_then_variadic_args_t{}, _STD forward<_Other2>(_Ax), _STD forward<_Other1>(_Val)) {}
// dtor non-virtual due to _Delete_this()
private:
_Mybase* _Copy(void* _Where) const override {
auto& _Myax = _Mypair._Get_first();
if constexpr (_Is_large<_Func_impl>) {
_Myalty _Rebound(_Myax);
_Alloc_construct_ptr<_Myalty> _Constructor{_Rebound};
_Constructor._Allocate();
_Construct_in_place(*_Constructor._Ptr, _Mypair._Myval2, _Myax);
return _Constructor._Release();
} else {
const auto _Ptr = static_cast<_Func_impl*>(_Where);
_Construct_in_place(*_Ptr, _Mypair._Myval2, _Myax);
return _Ptr;
}
}
_Mybase* _Move(void* _Where) noexcept override {
if constexpr (_Is_large<_Func_impl>) {
return nullptr;
} else {
const auto _Ptr = static_cast<_Func_impl*>(_Where);
_Construct_in_place(*_Ptr, _STD move(_Mypair._Myval2), _STD move(_Mypair._Get_first()));
return _Ptr;
}
}
_Rx _Do_call(_Types&&... _Args) override { // call wrapped function
if constexpr (is_void_v<_Rx>) {
(void) _STD invoke(_Mypair._Myval2, _STD forward<_Types>(_Args)...);
} else {
return _STD invoke(_Mypair._Myval2, _STD forward<_Types>(_Args)...);
}
}
const type_info& _Target_type() const noexcept override {
#if _HAS_STATIC_RTTI
return typeid(_Callable);
#else // _HAS_STATIC_RTTI
_CSTD abort();
#endif // _HAS_STATIC_RTTI
}
const void* _Get() const noexcept override {
return _STD addressof(_Mypair._Myval2);
}
void _Delete_this(bool _Deallocate) noexcept override { // destroy self
_Myalty _Al(_Mypair._Get_first());
_Destroy_in_place(*this);
if (_Deallocate) {
_Deallocate_plain(_Al, this);
}
}
_Compressed_pair<_Alloc, _Callable> _Mypair;
};
#endif // _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Callable, class _Rx, class... _Types>
class _Func_impl_no_alloc final : public _Func_base<_Rx, _Types...> {
// derived class for specific implementation types that don't use allocators
public:
using _Mybase = _Func_base<_Rx, _Types...>;
using _Nothrow_move = is_nothrow_move_constructible<_Callable>;
template <class _Other, enable_if_t<!is_same_v<_Func_impl_no_alloc, decay_t<_Other>>, int> = 0>
explicit _Func_impl_no_alloc(_Other&& _Val) : _Callee(_STD forward<_Other>(_Val)) {}
// dtor non-virtual due to _Delete_this()
private:
_Mybase* _Copy(void* _Where) const override {
if constexpr (_Is_large<_Func_impl_no_alloc>) {
return _Global_new<_Func_impl_no_alloc>(_Callee);
} else {
return ::new (_Where) _Func_impl_no_alloc(_Callee);
}
}
_Mybase* _Move(void* _Where) noexcept override {
if constexpr (_Is_large<_Func_impl_no_alloc>) {
return nullptr;
} else {
return ::new (_Where) _Func_impl_no_alloc(_STD move(_Callee));
}
}
_Rx _Do_call(_Types&&... _Args) override { // call wrapped function
if constexpr (is_void_v<_Rx>) {
(void) _STD invoke(_Callee, _STD forward<_Types>(_Args)...);
} else {
return _STD invoke(_Callee, _STD forward<_Types>(_Args)...);
}
}
const type_info& _Target_type() const noexcept override {
#if _HAS_STATIC_RTTI
return typeid(_Callable);
#else // _HAS_STATIC_RTTI
_CSTD abort();
#endif // _HAS_STATIC_RTTI
}
const void* _Get() const noexcept override {
return _STD addressof(_Callee);
}
void _Delete_this(bool _Dealloc) noexcept override { // destroy self
this->~_Func_impl_no_alloc();
if (_Dealloc) {
_Deallocate<alignof(_Func_impl_no_alloc)>(this, sizeof(_Func_impl_no_alloc));
}
}
_Callable _Callee;
};
template <class _Ret, class... _Types>
class _Func_class : public _Arg_types<_Types...> {
public:
using result_type = _Ret;
using _Ptrt = _Func_base<_Ret, _Types...>;
_Func_class() noexcept {
_Set(nullptr);
}
_Ret operator()(_Types... _Args) const {
if (_Empty()) {
_Xbad_function_call();
}
const auto _Impl = _Getimpl();
return _Impl->_Do_call(_STD forward<_Types>(_Args)...);
}
~_Func_class() noexcept {
_Tidy();
}
protected:
template <class _Fx, class _Function>
using _Enable_if_callable_t = enable_if_t<conjunction_v<negation<is_same<_Remove_cvref_t<_Fx>, _Function>>,
_Is_invocable_r<_Ret, decay_t<_Fx>&, _Types...>>,
int>;
bool _Empty() const noexcept {
return !_Getimpl();
}
void _Reset_copy(const _Func_class& _Right) { // copy _Right's stored object
if (!_Right._Empty()) {
_Set(_Right._Getimpl()->_Copy(&_Mystorage));
}
}
void _Reset_move(_Func_class&& _Right) noexcept { // move _Right's stored object
if (!_Right._Empty()) {
if (_Right._Local()) { // move and tidy
_Set(_Right._Getimpl()->_Move(&_Mystorage));
_Right._Tidy();
} else { // steal from _Right
_Set(_Right._Getimpl());
_Right._Set(nullptr);
}
}
}
template <class _Fx>
void _Reset(_Fx&& _Val) { // store copy of _Val
if (!_Test_callable(_Val)) { // null member pointer/function pointer/std::function
return; // already empty
}
using _Impl = _Func_impl_no_alloc<decay_t<_Fx>, _Ret, _Types...>;
if constexpr (_Is_large<_Impl>) {
// dynamically allocate _Val
_Set(_Global_new<_Impl>(_STD forward<_Fx>(_Val)));
} else {
// store _Val in-situ
_Set(::new (static_cast<void*>(&_Mystorage)) _Impl(_STD forward<_Fx>(_Val)));
}
}
#if _HAS_FUNCTION_ALLOCATOR_SUPPORT
template <class _Fx, class _Alloc>
void _Reset_alloc(_Fx&& _Val, const _Alloc& _Ax) { // store copy of _Val with allocator
if (!_Test_callable(_Val)) { // null member pointer/function pointer/std::function
return; // already empty
}
using _Myimpl = _Func_impl<decay_t<_Fx>, _Alloc, _Ret, _Types...>;
if constexpr (_Is_large<_Myimpl>) {
// dynamically allocate _Val
using _Alimpl = _Rebind_alloc_t<_Alloc, _Myimpl>;
_Alimpl _Al(_Ax);
_Alloc_construct_ptr<_Alimpl> _Constructor{_Al};
_Constructor._Allocate();
_Construct_in_place(*_Constructor._Ptr, _STD forward<_Fx>(_Val), _Ax);
_Set(_Unfancy(_Constructor._Release()));
} else {
// store _Val in-situ
const auto _Ptr = reinterpret_cast<_Myimpl*>(&_Mystorage);
_Construct_in_place(*_Ptr, _STD forward<_Fx>(_Val), _Ax);
_Set(_Ptr);
}
}
#endif // _HAS_FUNCTION_ALLOCATOR_SUPPORT
void _Tidy() noexcept {
if (!_Empty()) { // destroy callable object and maybe delete it
_Getimpl()->_Delete_this(!_Local());
_Set(nullptr);
}
}
void _Swap(_Func_class& _Right) noexcept { // swap contents with contents of _Right
if (!_Local() && !_Right._Local()) { // just swap pointers
_Ptrt* _Temp = _Getimpl();
_Set(_Right._Getimpl());
_Right._Set(_Temp);
} else { // do three-way move
_Func_class _Temp;
_Temp._Reset_move(_STD move(*this));
_Reset_move(_STD move(_Right));
_Right._Reset_move(_STD move(_Temp));
}
}
#if _HAS_STATIC_RTTI
const type_info& _Target_type() const noexcept {
return _Getimpl() ? _Getimpl()->_Target_type() : typeid(void);
}
const void* _Target(const type_info& _Info) const noexcept {
return _Getimpl() ? _Getimpl()->_Target(_Info) : nullptr;
}
#endif // _HAS_STATIC_RTTI
private:
bool _Local() const noexcept { // test for locally stored copy of object
return _Getimpl() == static_cast<const void*>(&_Mystorage);
}
union _Storage { // storage for small objects (basic_string is small)
max_align_t _Dummy1; // for maximum alignment
char _Dummy2[_Space_size]; // to permit aliasing
_Ptrt* _Ptrs[_Small_object_num_ptrs]; // _Ptrs[_Small_object_num_ptrs - 1] is reserved
};
_Storage _Mystorage;