-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
fplus.hpp
16078 lines (14468 loc) · 549 KB
/
fplus.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
#pragma once
//
// fplus.hpp
//
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// benchmark_session.hpp
//
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// container_common.hpp
//
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// compare.hpp
//
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// composition.hpp
//
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// function_traits.hpp
//
//--------------------------------------
// utils/traits: Additional type traits
//--------------------------------------
//
// Copyright kennytm (auraHT Ltd.) 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
/**
``<utils/traits.hpp>`` --- Additional type traits
=================================================
This module provides additional type traits and related functions, missing from
the standard library.
*/
#ifndef TRAITS_HPP_9ALQFEFX7TO
#define TRAITS_HPP_9ALQFEFX7TO 1
#include <cstdlib>
#include <functional>
#include <tuple>
#include <type_traits>
//
// internal/meta.hpp
//
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <type_traits>
namespace fplus {
namespace internal {
// C++14 compatible void_t (http://en.cppreference.com/w/cpp/types/void_t)
template <typename... Ts>
struct make_void {
using type = void;
};
template <typename... Ts>
using void_t = typename make_void<Ts...>::type;
// Sometimes you don't want to use std::decay_t, and the temptation of short
// writing can be huge...
template <typename T>
using uncvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
// disjunction/conjunction/negation, useful to short circuit SFINAE checks
// Use with parsimony, MSVC 2015 can have ICEs quite easily
template <typename...>
struct disjunction : std::false_type {
};
template <typename B1>
struct disjunction<B1> : B1 {
};
template <typename B1, typename... Bn>
struct disjunction<B1, Bn...>
: std::conditional<bool(B1::value), B1, disjunction<Bn...>>::type {
};
template <typename...>
struct conjunction : std::true_type {
};
template <typename B1>
struct conjunction<B1> : B1 {
};
template <typename B1, typename... Bn>
struct conjunction<B1, Bn...>
: std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {
};
template <typename B>
struct negation : std::integral_constant<bool, !bool(B::value)> {
};
// non short-circuiting meta functions
// source: https://stackoverflow.com/a/27221517/4116453
template <bool...>
struct bool_pack;
template <bool... Values>
struct all_of
: std::is_same<bool_pack<Values..., true>, bool_pack<true, Values...>> {
};
// there seems to be a bug in libc++'s std::is_function
// provide our own (cppreference one)
// (the MSVC implementation seems correct)
#ifndef _MSC_VER
#define PROVIDE_IS_FUNCTION_POLYFILL
#endif
#ifndef PROVIDE_IS_FUNCTION_POLYFILL
template <class... Any>
using is_function = std::is_function<Any...>;
#else // PROVIDE_IS_FUNCTION_POLYFILL
// primary template
template <class>
struct is_function : std::false_type {
};
// specialization for regular functions
template <class Ret, class... Args>
struct is_function<Ret(Args...)> : std::true_type {
};
// specialization for variadic functions such as std::printf
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...)> : std::true_type {
};
// specialization for function types that have cv-qualifiers
template <class Ret, class... Args>
struct is_function<Ret(Args...) const> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args...) volatile> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args...) const volatile> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...) const> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...) volatile> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...) const volatile> : std::true_type {
};
// specialization for function types that have ref-qualifiers
template <class Ret, class... Args>
struct is_function<Ret(Args...)&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args...) const&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args...) volatile&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args...) const volatile&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...)&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...) const&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...) volatile&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...) const volatile&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args...) &&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args...) const&&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args...) volatile&&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args...) const volatile&&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...) &&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...) const&&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...) volatile&&> : std::true_type {
};
template <class Ret, class... Args>
struct is_function<Ret(Args..., ...) const volatile&&> : std::true_type {
};
#endif // PROVIDE_IS_FUNCTION_POLYFILL
template <typename>
struct reverse_integer_sequence_impl;
template <typename T>
struct reverse_integer_sequence_impl<std::integer_sequence<T>>
: std::integer_sequence<T> {
};
template <typename T, T... Ints>
struct reverse_integer_sequence_impl<std::integer_sequence<T, Ints...>>
: std::integer_sequence<T, sizeof...(Ints) - 1 - Ints...> {
};
template <typename Seq>
using reverse_integer_sequence = reverse_integer_sequence_impl<Seq>;
template <typename T, T N>
using make_reverse_integer_sequence = reverse_integer_sequence<std::make_integer_sequence<T, N>>;
template <std::size_t... Idx>
using reverse_index_sequence = reverse_integer_sequence<std::index_sequence<Idx...>>;
template <std::size_t N>
using make_reverse_index_sequence = make_reverse_integer_sequence<std::size_t, N>;
}
}
namespace fplus {
// source: https://github.com/kennytm/utils
namespace utils {
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#endif
/**
.. macro:: DECLARE_HAS_TYPE_MEMBER(member_name)
This macro declares a template ``has_member_name`` which will check whether
a type member ``member_name`` exists in a particular type.
Example::
DECLARE_HAS_TYPE_MEMBER(result_type)
...
printf("%d\n", has_result_type< std::plus<int> >::value);
// ^ prints '1' (true)
printf("%d\n", has_result_type< double(*)() >::value);
// ^ prints '0' (false)
*/
#define DECLARE_HAS_TYPE_MEMBER(member_name) \
template <typename, typename = void> \
struct has_##member_name { \
enum { value = false }; \
}; \
template <typename T> \
struct has_##member_name<T, typename std::enable_if<sizeof(typename T::member_name) || true>::type> { \
enum { value = true }; \
};
/**
.. type:: struct utils::function_traits<F>
Obtain compile-time information about a function object *F*.
This template currently supports the following types:
* Normal function types (``R(T...)``), function pointers (``R(*)(T...)``)
and function references (``R(&)(T...)`` and ``R(&&)(T...)``).
* Member functions (``R(C::*)(T...)``)
* ``std::function<F>``
* Type of lambda functions, and any other types that has a unique
``operator()``.
* Type of ``std::mem_fn`` (only for GCC's libstdc++ and LLVM's libc++).
Following the C++ spec, the first argument will be a raw pointer.
*/
template <typename T>
struct function_traits
: public function_traits<decltype(&T::operator())> {
};
namespace xx_impl {
template <typename C, typename R, typename... A>
struct memfn_type {
typedef typename std::conditional<
std::is_const<C>::value,
typename std::conditional<
std::is_volatile<C>::value,
R (C::*)(A...) const volatile,
R (C::*)(A...) const>::type,
typename std::conditional<
std::is_volatile<C>::value,
R (C::*)(A...) volatile,
R (C::*)(A...)>::type>::type type;
};
}
template <typename ReturnType, typename... Args>
struct function_traits<ReturnType(Args...)> {
/**
.. type:: type result_type
The type returned by calling an instance of the function object type *F*.
*/
typedef ReturnType result_type;
/**
.. type:: type function_type
The function type (``R(T...)``).
*/
typedef ReturnType function_type(Args...);
/**
.. type:: type member_function_type<OwnerType>
The member function type for an *OwnerType* (``R(OwnerType::*)(T...)``).
*/
template <typename OwnerType>
using member_function_type = typename xx_impl::memfn_type<
typename std::remove_pointer<typename std::remove_reference<OwnerType>::type>::type,
ReturnType, Args...>::type;
/**
.. data:: static const size_t arity
Number of arguments the function object will take.
*/
enum { arity = sizeof...(Args) };
/**
.. type:: type arg<n>::type
The type of the *n*-th argument.
*/
template <size_t i>
struct arg {
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
};
};
#if __cplusplus > 201510L
template <typename ReturnType, typename... Args>
struct function_traits<ReturnType(Args...) noexcept>
: public function_traits<ReturnType(Args...)> {
};
#endif
template <typename ReturnType, typename... Args>
struct function_traits<ReturnType (*)(Args...)>
: public function_traits<ReturnType(Args...)> {
};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType (ClassType::*)(Args...)>
: public function_traits<ReturnType(Args...)> {
typedef ClassType& owner_type;
};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType (ClassType::*)(Args...) const>
: public function_traits<ReturnType(Args...)> {
typedef const ClassType& owner_type;
};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType (ClassType::*)(Args...) volatile>
: public function_traits<ReturnType(Args...)> {
typedef volatile ClassType& owner_type;
};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType (ClassType::*)(Args...) const volatile>
: public function_traits<ReturnType(Args...)> {
typedef const volatile ClassType& owner_type;
};
#if __cplusplus > 201510L
template <typename ReturnType, typename... Args>
struct function_traits<ReturnType (*)(Args...) noexcept>
: public function_traits<ReturnType(Args...)> {
};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType (ClassType::*)(Args...) noexcept>
: public function_traits<ReturnType(Args...)> {
typedef ClassType& owner_type;
};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType (ClassType::*)(Args...) const noexcept>
: public function_traits<ReturnType(Args...)> {
typedef const ClassType& owner_type;
};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType (ClassType::*)(Args...) volatile noexcept>
: public function_traits<ReturnType(Args...)> {
typedef volatile ClassType& owner_type;
};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType (ClassType::*)(Args...) const volatile noexcept>
: public function_traits<ReturnType(Args...)> {
typedef const volatile ClassType& owner_type;
};
#endif
template <typename FunctionType>
struct function_traits<std::function<FunctionType>>
: public function_traits<FunctionType> {
};
#if defined(_GLIBCXX_FUNCTIONAL)
#define MEM_FN_SYMBOL_XX0SL7G4Z0J std::_Mem_fn
#elif defined(_LIBCPP_FUNCTIONAL)
#define MEM_FN_SYMBOL_XX0SL7G4Z0J std::__mem_fn
#endif
#ifdef MEM_FN_SYMBOL_XX0SL7G4Z0J
template <typename R, typename C>
struct function_traits<MEM_FN_SYMBOL_XX0SL7G4Z0J<R (C::*)()>>
: public function_traits<R(C*)> {
};
template <typename R, typename C, typename... A>
struct function_traits<MEM_FN_SYMBOL_XX0SL7G4Z0J<R (C::*)(A...)>>
: public function_traits<R(C*, A...)> {
};
template <typename R, typename C, typename... A>
struct function_traits<MEM_FN_SYMBOL_XX0SL7G4Z0J<R (C::*)(A...) const>>
: public function_traits<R(const C*, A...)> {
};
template <typename R, typename C, typename... A>
struct function_traits<MEM_FN_SYMBOL_XX0SL7G4Z0J<R (C::*)(A...) volatile>>
: public function_traits<R(volatile C*, A...)> {
};
template <typename R, typename C, typename... A>
struct function_traits<MEM_FN_SYMBOL_XX0SL7G4Z0J<R (C::*)(A...) const volatile>>
: public function_traits<R(const volatile C*, A...)> {
};
#if __cplusplus > 201510L
template <typename R, typename C>
struct function_traits<MEM_FN_SYMBOL_XX0SL7G4Z0J<R (C::*)() noexcept>>
: public function_traits<R(C*)> {
};
template <typename R, typename C, typename... A>
struct function_traits<MEM_FN_SYMBOL_XX0SL7G4Z0J<R (C::*)(A...) noexcept>>
: public function_traits<R(C*, A...)> {
};
template <typename R, typename C, typename... A>
struct function_traits<MEM_FN_SYMBOL_XX0SL7G4Z0J<R (C::*)(A...) const noexcept>>
: public function_traits<R(const C*, A...)> {
};
template <typename R, typename C, typename... A>
struct function_traits<MEM_FN_SYMBOL_XX0SL7G4Z0J<R (C::*)(A...) volatile noexcept>>
: public function_traits<R(volatile C*, A...)> {
};
template <typename R, typename C, typename... A>
struct function_traits<MEM_FN_SYMBOL_XX0SL7G4Z0J<R (C::*)(A...) const volatile noexcept>>
: public function_traits<R(const volatile C*, A...)> {
};
#endif
#undef MEM_FN_SYMBOL_XX0SL7G4Z0J
#endif
template <typename T>
struct function_traits<T&> : public function_traits<T> {
};
template <typename T>
struct function_traits<const T&> : public function_traits<T> {
};
template <typename T>
struct function_traits<volatile T&> : public function_traits<T> {
};
template <typename T>
struct function_traits<const volatile T&> : public function_traits<T> {
};
template <typename T>
struct function_traits<T&&> : public function_traits<T> {
};
template <typename T>
struct function_traits<const T&&> : public function_traits<T> {
};
template <typename T>
struct function_traits<volatile T&&> : public function_traits<T> {
};
template <typename T>
struct function_traits<const volatile T&&> : public function_traits<T> {
};
#define FORWARD_RES_8QR485JMSBT \
typename std::conditional< \
std::is_lvalue_reference<R>::value, \
T&, \
typename std::remove_reference<T>::type&&>::type
/**
.. function:: auto utils::forward_like<Like, T>(T&& t) noexcept
Forward the reference *t* like the type of *Like*. That means, if *Like* is
an lvalue (reference), this function will return an lvalue reference of *t*.
Otherwise, if *Like* is an rvalue, this function will return an rvalue
reference of *t*.
This is mainly used to propagate the expression category (lvalue/rvalue) of
a member of *Like*, generalizing ``std::forward``.
*/
template <typename R, typename T>
FORWARD_RES_8QR485JMSBT forward_like(T&& input) noexcept
{
return static_cast<FORWARD_RES_8QR485JMSBT>(input);
}
#undef FORWARD_RES_8QR485JMSBT
/**
.. type:: struct utils::copy_cv<From, To>
Copy the CV qualifier between the two types. For example,
``utils::copy_cv<const int, double>::type`` will become ``const double``.
*/
template <typename From, typename To>
struct copy_cv {
private:
typedef typename std::remove_cv<To>::type raw_To;
typedef typename std::conditional<std::is_const<From>::value,
const raw_To, raw_To>::type const_raw_To;
public:
/**
.. type:: type type
Result of cv-copying.
*/
typedef typename std::conditional<std::is_volatile<From>::value,
volatile const_raw_To, const_raw_To>::type type;
};
/**
.. type:: struct utils::pointee<T>
Returns the type by derefering an instance of *T*. This is a generalization
of ``std::remove_pointer``, that it also works with iterators.
*/
template <typename T>
struct pointee {
/**
.. type:: type type
Result of dereferencing.
*/
typedef typename std::remove_reference<decltype(*std::declval<T>())>::type type;
};
/**
.. function:: std::add_rvalue_reference<T>::type utils::rt_val<T>() noexcept
Returns a value of type *T*. It is guaranteed to do nothing and will not
throw a compile-time error, but using the returned result will cause
undefined behavior.
*/
template <typename T>
typename std::add_rvalue_reference<T>::type rt_val() noexcept
{
return std::move(*static_cast<T*>(nullptr));
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
}
}
namespace fplus {
namespace internal {
template <typename>
struct is_std_function : std::false_type {
};
template <typename T>
struct is_std_function<std::function<T>> : std::true_type {
};
// Those traits are needed to not perform arity checks on a generic-lambd
// or a templated/overloaded operator()
template <typename T, typename = void>
struct has_function_traits : std::false_type {
};
// There is a bug with GCC 7 when a std::function is passed as T.
// It produces an ambiguous call between this one and the std::function overload
// It's related to our void_t implementation, the C++14 compatible version does not
// work, whereas the C++17 one does...
//
// So, help GCC a bit with is_std_function
template <typename T>
struct has_function_traits<T,
std::enable_if_t<!is_std_function<T>::value,
void_t<decltype(&T::operator())>>>
: std::true_type {
};
template <typename ReturnType, typename... Args>
struct has_function_traits<ReturnType(Args...)> : std::true_type {
};
template <typename ReturnType, typename... Args>
struct has_function_traits<ReturnType (*)(Args...)> : std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...)> : std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) volatile>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const volatile>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...)&> : std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const&>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) volatile&>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const volatile&>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) &&>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const&&>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) volatile&&>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const volatile&&>
: std::true_type {
};
template <typename FunctionType>
struct has_function_traits<std::function<FunctionType>> : std::true_type {
};
#if __cplusplus > 201510L
template <typename ReturnType, typename... Args>
struct has_function_traits<ReturnType(Args...) noexcept> : std::true_type {
};
template <typename ReturnType, typename... Args>
struct has_function_traits<ReturnType (*)(Args...) noexcept> : std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) noexcept> : std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const noexcept>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) volatile noexcept>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const volatile noexcept>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) & noexcept> : std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const & noexcept>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) volatile & noexcept>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const volatile & noexcept>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) && noexcept>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const && noexcept>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) volatile && noexcept>
: std::true_type {
};
template <typename ReturnType, typename ClassType, typename... Args>
struct has_function_traits<ReturnType (ClassType::*)(Args...) const volatile && noexcept>
: std::true_type {
};
#endif
}
}
#endif
//
// internal/apply.hpp
//
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <tuple>
#include <type_traits>
#include <utility>
//
// internal/invoke.hpp
//
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <functional>
#include <type_traits>
#include <utility>
// borrowed to libc++
#define FPLUS_INVOKE_RETURN(...) \
->decltype(__VA_ARGS__) \
{ \
return __VA_ARGS__; \
}
namespace fplus {
namespace internal {
// We need std::invoke to detect callable objects
//
// source:
// http://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/invoke&oldid=82514
template <typename U>
static std::true_type is_refwrap_test(const std::reference_wrapper<U>&);
template <typename U>
static std::false_type is_refwrap_test(const U&);
template <typename T>
struct is_reference_wrapper : decltype(is_refwrap_test(std::declval<T>())) {
};
template <typename T, typename U = typename std::decay<T>::type>
struct unwrap_reference_wrapper {
using type = T;
};
template <typename T, typename U>
struct unwrap_reference_wrapper<T, std::reference_wrapper<U>> {
using type = U&;
};
template <typename T>
using unwrap_reference_wrapper_t = typename unwrap_reference_wrapper<T>::type;
// note: clang only triggers the second static_assert
// - static_assert(is_invocable<&base_class::non_const_method, const derived_class&>::value, "");
// - static_assert(is_invocable<&base_class::non_const_method, const base_class&>::value, "");
// GCC triggers both. To workaround this clang bug, we have to manage cv correctness ourselves
template <typename T>
struct is_const_member_function : std::false_type {
};
// decay doesn't add pointer to abominable functions, don't bother writing them
template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const> : std::true_type {
};
template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const&> : std::true_type {
};
template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const&&> : std::true_type {
};
template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const volatile> : std::true_type {
};
template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const volatile&> : std::true_type {
};
template <typename R, typename... Args>
struct is_const_member_function<R(Args...) const volatile&&> : std::true_type {
};
template <typename T>
struct is_volatile_member_function : std::false_type {
};
// decay doesn't add pointer to abominable functions, don't bother writing them
template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) volatile> : std::true_type {
};
template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) volatile&> : std::true_type {
};
template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) volatile&&> : std::true_type {
};
template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) const volatile> : std::true_type {
};
template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) const volatile&> : std::true_type {
};
template <typename R, typename... Args>
struct is_volatile_member_function<R(Args...) const volatile&&> : std::true_type {
};
template <typename Object, typename Signature>
struct has_correct_cv {
// if object has no cv, every method can be called
// else the method must have the same cv than the object
static constexpr bool value = std::is_same<typename std::remove_cv<Object>::type, Object>::value || ((is_volatile_member_function<Signature>::value == std::is_volatile<Object>::value) && (is_const_member_function<Signature>::value == std::is_const<Object>::value));
};
// pointer to member function - reference to object
template <
typename Base,
typename T,
typename Derived,
typename... Args,
typename Unwrapped = unwrap_reference_wrapper_t<Derived>,
typename std::enable_if<
is_function<T>::value && has_correct_cv<typename std::remove_reference<Unwrapped>::type, T>::value && std::is_base_of<Base, typename std::decay<Unwrapped>::type>::value,
int>::type
= 0>
inline auto invoke_impl(T Base::*pmf, Derived&& ref, Args&&... args)
FPLUS_INVOKE_RETURN((std::forward<Unwrapped>(ref).*pmf)(std::forward<Args>(args)...))
// pointer to member function - pointer to object
template <
typename Base,
typename T,
typename Pointer,
typename... Args,
typename std::enable_if<
is_function<T>::value && has_correct_cv<typename std::remove_pointer<typename std::decay<Pointer>::type>::type, T>::value && !std::is_base_of<Base, typename std::decay<Pointer>::type>::value,
int>::type
= 0>
inline auto invoke_impl(T Base::*pmf, Pointer&& ptr, Args&&... args)
FPLUS_INVOKE_RETURN(((*std::forward<Pointer>(ptr)).*pmf)(std::forward<Args>(args)...))
// pointer to non-static data member - reference to object
template <
typename Base,
typename T,
typename Derived,
typename Unwrapped = unwrap_reference_wrapper_t<Derived>,
typename std::enable_if<
!is_function<T>::value && std::is_base_of<Base, typename std::decay<Unwrapped>::type>::value,
int>::type
= 0>
inline auto invoke_impl(T Base::*pmd, Derived&& ref)
FPLUS_INVOKE_RETURN((std::forward<Unwrapped>(ref).*pmd))
// pointer to non-static data member - pointer to object
template <
typename Base,
typename T,