forked from potswa/cxx_function
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cxx_function.hpp
1451 lines (1251 loc) · 70 KB
/
cxx_function.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
// cxx_function.hpp: major evolution for std::function
// Copyright 2015 by David Krauss.
// This source is released under the MIT license, http://opensource.org/licenses/MIT
#ifndef INCLUDED_CXX_FUNCTION_HPP
#define INCLUDED_CXX_FUNCTION_HPP
#include <cassert>
#include <cstring>
#include <exception>
#include <functional> // for std::bad_function_call and std::mem_fn
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
namespace cxx_function {
#if __cplusplus > 201610
template< typename t >
using in_place_t = std::in_place_type_t< t >;
template< typename t >
constexpr in_place_t< t > & in_place = std::in_place_type< t >;
#else
template< typename >
struct in_place_t {};
# if __cplusplus >= 201402
template< typename t >
constexpr in_place_t< t > in_place = {};
# endif
#endif
namespace impl {
#define UGLY( NAME ) CXX_FUNCTION_ ## NAME
#if __GNUC__ && ! __clang__ && __GNUC__ < 5
# define is_trivially_move_constructible has_trivial_copy_constructor
# define is_trivially_copy_constructible has_trivial_copy_constructor
# define deprecated(MSG) __attribute__((deprecated (MSG)))
# define OLD_GCC_FIX(...) __VA_ARGS__
# define OLD_GCC_SKIP(...)
#else
# define deprecated(MSG) [[deprecated (MSG)]]
# define OLD_GCC_FIX(...)
# define OLD_GCC_SKIP(...) __VA_ARGS__
#endif
#if _MSC_VER
# define MSVC_FIX(...) __VA_ARGS__
# define MSVC_SKIP(...)
#else
# define MSVC_FIX(...)
# define MSVC_SKIP(...) __VA_ARGS__
#endif
#define UNPACK(...) __VA_ARGS__
#define IGNORE(...)
#define DISPATCH_CQ( MACRO, UNSAFE, TYPE_QUALS, FN_QUALS ) \
MACRO( TYPE_QUALS, FN_QUALS, UNSAFE ) MACRO( const TYPE_QUALS, const FN_QUALS, IGNORE )
#define DISPATCH_CV( MACRO, UNSAFE, TYPE_QUALS, FN_QUALS ) \
DISPATCH_CQ( MACRO, UNSAFE, TYPE_QUALS, FN_QUALS ) DISPATCH_CQ( MACRO, IGNORE, volatile TYPE_QUALS, volatile FN_QUALS )
// Apply a given macro over all reference qualifications.
#define DISPATCH_CVREFQ( MACRO, TYPE_QUALS, FN_QUALS ) \
DISPATCH_CV( MACRO, IGNORE, & TYPE_QUALS, & FN_QUALS ) DISPATCH_CV( MACRO, IGNORE, && TYPE_QUALS, && FN_QUALS )
#define DISPATCH_CVOBJQ( MACRO, UNSAFE, TYPE_QUALS, FN_QUALS ) \
DISPATCH_CV( MACRO, UNSAFE, & TYPE_QUALS, FN_QUALS ) DISPATCH_CVREFQ( MACRO, TYPE_QUALS, FN_QUALS )
// Apply a given macro over all function qualifications.
#if __cpp_noexcept_function_type
# define DISPATCH_ALL( MACRO ) DISPATCH_CVOBJQ( MACRO, UNPACK, , ) DISPATCH_CVOBJQ( MACRO, IGNORE, , noexcept )
#else
# define DISPATCH_ALL( MACRO ) DISPATCH_CVOBJQ( MACRO, UNPACK, , )
#endif
#if __cpp_lib_experimental_logical_traits
using std::experimental::conjunction;
using std::experimental::negation;
#else
template< typename ... cond >
struct conjunction
: std::true_type {};
template< typename next, typename ... cond >
struct conjunction< next, cond ... >
: conjunction< typename next::type, cond ... >::type {}; // Not conforming: does not propagate critical type-value.
template< typename ... cond >
struct conjunction< std::true_type, cond ... >
: conjunction< cond ... >::type {};
template< typename ... cond >
struct conjunction< std::false_type, cond ... >
: std::false_type {};
template< typename cond >
struct negation
: std::integral_constant< bool, ! cond::value > {};
#endif
template< typename T >
T launder_cast( void * p )
{ return
#if __cpp_lib_launder || __clang_major__ >= 6 && __cplusplus >= 201703L
std::launder
#endif
( static_cast< T >( p ) ); }
template< typename T >
T launder_cast( void const * p )
{ return
#if __cpp_lib_launder || __clang_major__ >= 6 && __cplusplus >= 201703L
std::launder
#endif
( static_cast< T >( p ) ); }
// General-purpose dispatch tag.
template< typename ... > struct tag {};
// "Abstract" base class for the island inside the wrapper class, e.g. std::function.
// This must appear first in the wrapper class layout.
struct erasure_base {
struct erasure_utility const * table;
template< typename derived, typename copyable, typename ... sig >
erasure_base( tag< derived, copyable, sig ... > ); // Parameters are valueless tags for template deduction.
};
/* Implement a vtable using metaprogramming. Why?
1. Implement without polymorphic template instantiations (would need 2N of them).
2. Eliminate overhead and ABI issues associated with RTTI and weak linkage.
3. Allow static data entries as well as functions.
The table is stored as an aggregate, with a homegrown tuple.
Entries that would be trivial or useless may be set to nullptr.
Call dispatch entry types are normalized to non-member, unqualified form.
*/
struct erasure_utility {
void (* destructor)( erasure_base & );
void (* move_constructor_destructor)( erasure_base &&, void * dest, void const * source_alloc );
void (* copy_constructor)( erasure_base const &, void * dest, void const * alloc );
void const * (*target_access)( erasure_base const & );
std::type_info const & target_type;
std::type_info const * allocator_type;
};
template< typename ... free >
struct erasure_dispatch {};
template< typename free, typename ... rem >
struct erasure_dispatch< free, rem ... > {
free * call;
erasure_dispatch< rem ... > r;
};
template< typename ... free >
struct erasure_table {
erasure_utility utility; // Initial member of standard-layout struct.
erasure_dispatch< free ... > dispatch;
};
template< int x, typename free, typename ... rem >
typename std::enable_if< x == 0, free * >::type // Duplicate-type entries occur when overloads differ in qualification.
get( erasure_dispatch< free, rem ... > const & v ) // When this overload is enabled, it is more specialized.
{ return v.call; }
template< int x, typename free, typename ... list >
free * get( erasure_dispatch< list ... > const & v )
{ return get< x - 1, free >( v.r ); }
// Convert a member function signature to its free invocation counterpart.
template< typename sig >
struct member_to_free;
#define TYPE_CONVERT_CASE( TYPE_QUALS, FN_QUALS, UNSAFE ) \
template< typename ret, typename ... arg > \
struct member_to_free< ret( arg ... ) FN_QUALS > \
{ typedef ret type( erasure_base const &, arg && ... ); };
DISPATCH_ALL( TYPE_CONVERT_CASE )
#undef TYPE_CONVERT_CASE
// Allow early, eager conversion without incurring double conversion.
template< typename ret, typename ... arg >
struct member_to_free< ret( erasure_base const &, arg ... ) >
{ typedef ret type( erasure_base const &, arg && ... ); };
// Apply given cv-qualifiers and reference category to a new type.
template< typename source, typename target >
struct transfer_sig_qualifiers;
#define TRANSFER_QUALS_CASE( TYPE_QUALS, FN_QUALS, REFLESS ) \
template< typename ret, typename ... arg, typename target > \
struct transfer_sig_qualifiers< ret( arg ... ) FN_QUALS, target > \
{ typedef target TYPE_QUALS type; };
#define NOEXCEPT
DISPATCH_ALL( TRANSFER_QUALS_CASE )
#undef TRANSFER_QUALS_CASE
// Default, generic "virtual" functions to manage the wrapper payload lifetime.
template< typename derived >
struct erasure_special {
static void destroy( erasure_base & self ) noexcept
{ static_cast< derived & >( self ). ~ derived(); }
static void move( erasure_base && self, void * dest, void const * ) {
new (dest) derived( std::move( static_cast< derived & >( self ) ) );
destroy( self );
}
static void copy( erasure_base const & self, void * dest, void const * )
{ new (dest) derived( static_cast< derived const & >( self ) ); }
};
// These accessors generate "vtable" entries, but avoid instantiating functions that do not exist or would be trivial.
template< typename erasure >
struct erasure_trivially_destructible
: std::is_trivially_destructible< erasure > {};
template< typename derived >
constexpr typename std::enable_if< ! erasure_trivially_destructible< derived >::value >::type
( * erasure_destroy() ) ( erasure_base & )
{ return & derived::destroy; }
template< typename derived >
constexpr typename std::enable_if< erasure_trivially_destructible< derived >::value >::type
( * erasure_destroy() ) ( erasure_base & )
{ return nullptr; }
template< typename erasure >
struct erasure_trivially_movable : std::integral_constant< bool,
std::is_trivially_move_constructible< erasure >::value
&& std::is_trivially_destructible< erasure >::value > {};
template< typename derived >
constexpr typename std::enable_if< ! erasure_trivially_movable< derived >::value >::type
( * erasure_move() ) ( erasure_base &&, void *, void const * )
{ return & derived::move; }
template< typename derived >
constexpr typename std::enable_if< erasure_trivially_movable< derived >::value >::type
( * erasure_move() ) ( erasure_base &&, void *, void const * )
{ return nullptr; }
template< typename erasure >
struct erasure_nontrivially_copyable : std::integral_constant< bool,
std::is_copy_constructible< erasure >::value
&& ! std::is_trivially_copy_constructible< erasure >::value > {};
template< typename derived, typename enable >
constexpr typename std::enable_if< std::enable_if< enable::value, erasure_nontrivially_copyable< derived > >::type::value >::type
( * erasure_copy( enable * ) ) ( erasure_base const &, void *, void const * )
{ return & derived::copy; }
template< typename derived >
constexpr void ( * erasure_copy( ... ) ) ( erasure_base const &, void *, void const * )
{ return nullptr; }
// Collect the above into global tables.
// typename copyable enables non-trivial copyability. Trivially copyable and noncopyable tables both set it to false.
// For trivial, pointer-like target types, sig is actually the sequence of free function types.
#define VTABLE_INITIALIZER = { \
erasure_destroy< erasure >(), \
erasure_move< erasure >(), \
erasure_copy< erasure >( static_cast< copyable * >( nullptr ) ), \
& erasure::target_access, \
typeid (typename erasure::target_type), \
erasure::common_allocator_type_info, \
& erasure::template call< sig > ..., \
erasure_dispatch<>{} \
}
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wmissing-braces" // Need brace elision here.
#endif
template< typename erasure, typename copyable, typename ... sig >
struct make_erasure_table {
static MSVC_SKIP (constexpr) erasure_table< typename member_to_free< sig >::type ... > const value MSVC_SKIP( VTABLE_INITIALIZER );
};
#ifdef __GNUC__
# pragma GCC diagnostic pop
#endif
template< typename erasure, typename copyable, typename ... sig >
MSVC_SKIP (constexpr) erasure_table< typename member_to_free< sig >::type ... > const
make_erasure_table< erasure, copyable, sig ... >::value MSVC_FIX( VTABLE_INITIALIZER );
template< typename derived, typename copyable, typename ... sig >
erasure_base::erasure_base( tag< derived, copyable, sig ... > )
: table( & make_erasure_table< derived, copyable, sig ... >::value.utility ) {}
// Implement the uninitialized state.
struct null_erasure
: erasure_base // "vtable" interface class
, erasure_special< null_erasure > { // generic implementations of "virtual" functions
typedef void target_type;
static constexpr std::type_info const * common_allocator_type_info = nullptr;
// The const qualifier is bogus. Rather than type-erase an identical non-const version, let the wrapper do a const_cast.
static void const * target_access( erasure_base const & ) { return nullptr; } // target<void>() still returns nullptr.
template< typename ... sig >
null_erasure( tag< sig ... > ) noexcept
: erasure_base( tag< null_erasure, std::false_type, sig ... >{} ) {} // Initialize own "vtable pointer" at runtime.
template< typename, typename ret, typename ... arg >
static ret call( erasure_base const &, arg ... )
{ throw std::bad_function_call{}; }
};
// Implement erasures of function pointers and std::reference_wrapper, which avoid any allocation.
// For now, small user-defined target types piggyback here. The only concession is accepting a "copyable" flag.
template< typename in_target_type >
struct local_erasure
: erasure_base
, erasure_special< local_erasure< in_target_type > > {
typedef in_target_type target_type;
target_type target;
static constexpr std::type_info const * common_allocator_type_info = nullptr;
static void const * target_access( erasure_base const & self )
{ return & static_cast< local_erasure const & >( self ).target; }
template< typename copyable, typename ... free, typename ... arg >
local_erasure( tag< copyable, free ... >, arg && ... a )
: erasure_base( tag< local_erasure, copyable, free ... >{} )
, target( std::forward< arg >( a ) ... ) {}
template< typename sig, typename ret, typename ... arg >
static ret call( erasure_base const & self, arg ... a )
// Directly call the name "target," not a reference, to support devirtualization.
{ return ( (typename transfer_sig_qualifiers< sig, local_erasure >::type) self ).target( std::forward< arg >( a ) ... ); }
};
// Implement erasures of pointer-to-members, which need std::mem_fn instead of a direct call.
template< typename in_target_type >
struct ptm_erasure
: erasure_base
, erasure_special< ptm_erasure< in_target_type > > {
typedef in_target_type target_type;
target_type target; // Do not use mem_fn here...
static constexpr std::type_info const * common_allocator_type_info = nullptr;
static void const * target_access( erasure_base const & self )
{ return & static_cast< ptm_erasure const & >( self ).target; } // ... because the user can get read/write access to the target object.
template< typename ... free >
ptm_erasure( tag< free ... >, target_type a )
: erasure_base( tag< ptm_erasure, std::false_type, free ... >{} )
, target( a ) {}
template< typename, typename ret, typename ... arg >
static ret call( erasure_base const & self, arg ... a )
{ return ( std::mem_fn( static_cast< ptm_erasure const & >( self ).target ) )( std::forward< arg >( a ) ... ); }
};
// Implement erasures of objects that cannot be stored inside the wrapper.
/* This does still store the allocator and pointer in the wrapper. A more general case should be added.
However, there is a conundrum in rebinding an allocator to an instance of itself.
Also, it's not clear that a native pointer will always be stable, as opposed to a fancy pointer.
Fancy pointers exceeding the wrapper storage, with varying underlying referent storage, are another conundrum. */
// Use Allocator<char> as a common reference point, for the typeid operator and the instance in function_container.
// (The instance in the erasure object is always a bona fide Allocator<T>, though.)
template< typename allocator >
using common_allocator_rebind = typename std::allocator_traits< allocator >::template rebind_alloc< char >;
template< typename t, typename = void >
struct not_always_equal_by_expression : std::true_type {};
template< typename t >
using id_t = t; // MSVC can't parse or SFINAE on a nested-name-specifier with decltype.
template< typename t >
struct not_always_equal_by_expression< t, typename std::enable_if< id_t< decltype (std::declval< t >() == std::declval< t >()) >::value >::type >
: std::false_type {};
template< typename t, typename = void >
struct not_always_equal_by_trait : std::true_type {};
template< typename t >
struct not_always_equal_by_trait< t, typename std::enable_if< std::allocator_traits< t >::is_always_equal::value >::type >
: std::false_type {};
template< typename t >
struct is_always_equal : negation< conjunction< not_always_equal_by_trait< t >, not_always_equal_by_expression< t > > > {};
template< typename t >
struct is_always_equal< std::allocator< t > > : std::true_type {};
template< typename allocator_in, typename in_target_type >
struct allocator_erasure
: erasure_base
, allocator_in { // empty base class optimization (EBCO)
typedef std::allocator_traits< allocator_in > allocator_traits;
typedef common_allocator_rebind< allocator_in > common_allocator;
static MSVC_SKIP (constexpr) std::type_info const * const common_allocator_type_info
MSVC_SKIP ( = & typeid (allocator_erasure::common_allocator) );
typedef in_target_type target_type;
typename allocator_traits::pointer target;
allocator_in & alloc() { return static_cast< allocator_in & >( * this ); }
allocator_in const & alloc() const { return static_cast< allocator_in const & >( * this ); }
target_type * target_address() { return std::addressof( * target ); }
static void const * target_access( erasure_base const & self )
{ return std::addressof( * static_cast< allocator_erasure const & >( self ).target ); }
template< typename ... arg >
void construct_safely( arg && ... a ) try {
allocator_traits::construct( alloc(), target_address(), std::forward< arg >( a ) ... );
} catch (...) {
allocator_traits::deallocate( alloc(), target, 1 ); // Does not throw according to [allocator.requirements] §17.6.3.5 and DR2384.
throw;
} // The wrapper allocator instance cannot be updated following a failed initialization because the erasure allocator is already gone.
allocator_erasure( allocator_erasure && ) = default; // Called by move( true_type{}, ... ) when allocator or pointer is nontrivially movable.
allocator_erasure( allocator_erasure const & ) = delete;
template< typename copyable, typename ... sig, typename ... arg >
allocator_erasure( tag< copyable, sig ... >, std::allocator_arg_t, allocator_in const & in_alloc, arg && ... a )
: erasure_base( tag< allocator_erasure, copyable, sig ... >{} )
, allocator_in( in_alloc )
, target( allocator_traits::allocate( alloc(), 1 ) )
{ construct_safely( std::forward< arg >( a ) ... ); }
private:
// Copy or move. Moving only occurs to a different pool.
template< typename source >
allocator_erasure( std::allocator_arg_t, allocator_in const & dest_allocator, source && o )
: erasure_base( o )
, allocator_in( dest_allocator )
, target( allocator_traits::allocate( alloc(), 1 ) ) {
construct_safely( std::forward< typename std::conditional< std::is_lvalue_reference< source >::value,
target_type const &, target_type && >::type >( * o.target ) );
}
public:
void move( std::true_type, void * dest, void const * ) noexcept { // Call ordinary move constructor.
new (dest) allocator_erasure( std::move( * this ) ); // Move the pointer, not the object. Don't call the allocator at all.
this-> ~ allocator_erasure();
}
void move( std::false_type, void * dest, void const * dest_allocator_v ) {
auto * dest_allocator_p = static_cast< common_allocator const * >( dest_allocator_v ); // The wrapper verified the safety of this using typeid.
if ( ! dest_allocator_p || * dest_allocator_p == alloc() ) {
move( std::true_type{}, dest, dest_allocator_v ); // same pool
} else { // different pool
new (dest) allocator_erasure( std::allocator_arg, static_cast< allocator_in >( * dest_allocator_p ), // Reallocate.
std::move_if_noexcept( * this ) ); // Protect user against their own throwing move constructors.
destroy( * this );
}
}
// [*_]allocator_v points to the wrapper allocator instance, if any.
static void move( erasure_base && self_base, void * dest, void const * dest_allocator_v ) {
auto & self = static_cast< allocator_erasure & >( self_base );
// is_always_equal is usually false here, because it correlates with triviality which short-circuits this function.
std::move( self ).move( MSVC_FIX (impl::) is_always_equal< allocator_in >{}, dest, dest_allocator_v );
}
static void copy( erasure_base const & self_base, void * dest, void const * dest_allocator_v ) {
auto & self = static_cast< allocator_erasure const & >( self_base );
// Structure the control flow differently to avoid instantiating the copy constructor.
new (dest) allocator_erasure( std::allocator_arg,
dest_allocator_v?
static_cast< allocator_in const & >( * static_cast< common_allocator const * >( dest_allocator_v ) )
: self.alloc(),
self );
}
static void destroy( erasure_base & self_base ) noexcept {
auto & self = static_cast< allocator_erasure & >( self_base );
allocator_traits::destroy( self.alloc(), self.target_address() );
allocator_traits::deallocate( self.alloc(), self.target, 1 );
self. ~ allocator_erasure();
}
template< typename sig, typename ret, typename ... arg >
static ret call( erasure_base const & self, arg ... a ) {
return std::forward< typename transfer_sig_qualifiers< sig, target_type >::type >
( * static_cast< allocator_erasure const & >( self ).target )( std::forward< arg >( a ) ... );
}
};
template< typename allocator_in, typename in_target_type >
MSVC_SKIP (constexpr) std::type_info const * const allocator_erasure< allocator_in, in_target_type >::common_allocator_type_info
MSVC_FIX ( = & typeid (allocator_erasure::common_allocator) );
template< typename allocator, typename target_type >
struct erasure_trivially_destructible< allocator_erasure< allocator, target_type > >
: std::false_type {};
template< typename allocator, typename target_type >
struct erasure_trivially_movable< allocator_erasure< allocator, target_type > > : std::integral_constant< bool,
std::is_trivially_move_constructible< allocator_erasure< allocator, target_type > >::value
&& std::is_trivially_destructible< allocator_erasure< allocator, target_type > >::value
&& is_always_equal< allocator >::value > {};
template< typename allocator, typename target_type >
struct erasure_nontrivially_copyable< allocator_erasure< allocator, target_type > >
: std::is_copy_constructible< target_type > {};
// Forbid certain return value conversions that require temporaries.
template< typename from, typename to > // Primary case: from is object type. A prvalue is returned. Only allow user-defined conversions.
struct is_bad_return // Converting a prvalue to a reference binds a temporary unless the prvalue is a class and the reference is not a base.
: negation< conjunction< std::is_class< from >, negation< std::is_base_of< to, from > > > > {};
template< typename from, typename to > // Treat xvalues like lvalues.
struct is_bad_return< from &&, to > : is_bad_return< from &, to > {};
template< typename from, typename to > // From is reference type. Allow user-defined and derived-to-base conversions.
struct is_bad_return< from &, to > // Forbid converting an lvalue to a reference unless it's a class or the types match.
: conjunction< negation< std::is_same< typename std::remove_cv< from >::type, to > >, negation< std::is_class< from > > > {};
template< typename from, typename to >
struct is_safely_convertible
: conjunction<
negation< conjunction< // Prevent binding a return value to a temporary.
std::is_reference< to >,
is_bad_return< from, typename std::decay< to >::type >
> >,
std::is_convertible< from, to >
>::type {};
#if __cpp_lib_is_invocable || __cplusplus >= 201703L && __clang_major__ >= 6
# define INVOKE_RESULT( T, ARG ) invoke_result< T, ARG >
#else
# define INVOKE_RESULT( T, ARG ) result_of< T( ARG ) >
#endif
template< typename t, typename sig, typename = void >
struct is_callable : std::false_type {};
#define IS_CALLABLE_CASE( TYPE_QUALS, FN_QUALS, UNSAFE ) \
template< typename t, typename ret, typename ... arg > \
struct is_callable< t, ret( arg ... ) FN_QUALS, \
typename std::enable_if< is_safely_convertible< \
typename std::INVOKE_RESULT( t TYPE_QUALS, arg ... )::type \
, ret >::value >::type > \
: std::true_type {};
DISPATCH_CVOBJQ( IS_CALLABLE_CASE, IGNORE, , )
#undef IS_CALLABLE_CASE
#undef INVOKE_RESULT
template< typename sig >
struct is_callable< std::nullptr_t, sig >
: std::true_type {};
#if __cpp_noexcept_function_type
# if __cpp_lib_is_invocable || __clang_major__ >= 6
# define IS_NOTHROW_INVOKABLE( T, ARG, RET ) is_nothrow_invocable_r< RET, T, ARG >
# elif __GNUC__ && ! __clang__ && __GNUC__ < 7
# define IS_NOTHROW_INVOKABLE( T, ARG, RET ) is_nothrow_invocable< T( ARG ), RET >
# else
# define IS_NOTHROW_INVOKABLE( T, ARG, RET ) is_nothrow_callable< T( ARG ), RET >
# endif
# define NOEXCEPT_CASE( TYPE_QUALS, FN_QUALS, UNSAFE ) \
template< typename t, typename ret, typename ... arg > \
struct is_callable< t, ret( arg ... ) FN_QUALS noexcept, \
typename std::enable_if< std::IS_NOTHROW_INVOKABLE( t TYPE_QUALS, arg ..., ret )::value >::type > \
: is_callable< t, ret( arg ... ) FN_QUALS > {};
DISPATCH_CVOBJQ( NOEXCEPT_CASE, IGNORE, , )
# undef NOEXCEPT_CASE
# undef IS_NOTHROW_INVOKABLE
# define NOEXCEPT_NULLPTR_CASE( TYPE_QUALS, FN_QUALS, UNSAFE ) \
template< typename ret, typename ... arg > \
struct is_callable< std::nullptr_t, ret( arg ... ) FN_QUALS > : std::false_type {};
DISPATCH_CVOBJQ( NOEXCEPT_NULLPTR_CASE, IGNORE, , noexcept )
# undef NOEXCEPT_NULLPTR_CASE
#endif
template< typename ... sig >
struct is_all_callable {
template< typename t >
using temp = conjunction< is_callable< t, sig > ... >;
typedef std::false_type copies;
};
template< typename self, typename ... sig >
struct is_copyable_all_callable {
template< typename t, typename = void >
struct temp : conjunction< std::is_copy_constructible< t >, typename is_all_callable< sig ... >::template temp< t > >::type {};
template< typename v > // Presume that self is a copyable wrapper, since that is what uses this metafunction.
struct temp< self, v > : std::true_type {}; // Avoid inspecting incomplete self.
typedef std::true_type copies;
};
// Map privileged types to noexcept specifications.
template< typename source >
struct is_noexcept_erasable : std::false_type {};
template<>
struct is_noexcept_erasable< std::nullptr_t > : std::true_type {};
template< typename t >
struct is_noexcept_erasable< t * > : std::true_type {};
template< typename t, typename c >
struct is_noexcept_erasable< t c::* > : std::true_type {};
template< typename t >
struct is_noexcept_erasable< std::reference_wrapper< t > > : std::true_type {};
// Termination of the recursive template generated by WRAPPER_CASE.
template< typename derived, std::size_t n, typename ... sig >
struct wrapper_dispatch {
static_assert ( sizeof ... (sig) == 0, "An unsupported function signature was detected." );
void operator () ( wrapper_dispatch ) = delete; // Feed the "using operator ();" or "using call;" declaration in next derived class.
};
template< typename >
struct const_unsafe_case; // internal tag for function signatures introduced for backward compatibility of const-qualified access
// Generate the wrapper dispatcher by brute-force specialization over qualified function types.
// This macro generates a recursive template handling one type qualifier sequence, e.g. "volatile &" or "const."
// The final product converts a sequence of qualified signatures into an overload set, potentially with special cases for signatures of no qualification.
#define WRAPPER_CASE( \
TYPE_QUALS, FN_QUALS, /* The type qualifiers for this case. */ \
UNSAFE /* UNPACK if there are no qualifiers, IGNORE otherwise. Supports deprecated const-qualified access. */ \
) \
template< typename derived, std::size_t table_index, typename ret, typename ... arg, typename ... sig > \
struct wrapper_dispatch< derived, table_index, ret( arg ... ) FN_QUALS, sig ... > \
: wrapper_dispatch< derived, table_index+1, sig ... \
UNSAFE (, const_unsafe_case< ret( arg ... ) >) > { \
using wrapper_dispatch< derived, table_index+1, sig ... \
UNSAFE (, const_unsafe_case< ret( arg ... ) >) >::operator (); \
ret operator () ( arg ... a ) FN_QUALS \
{ return ( (derived const &) * this ).template call< table_index, ret >( std::forward< arg >( a ) ... ); } \
};
DISPATCH_ALL( WRAPPER_CASE )
#undef WRAPPER_CASE
// Additionally implement the legacy casting away of const, but with a warning.
template< typename derived, std::size_t n, typename ret, typename ... arg, typename ... more >
struct wrapper_dispatch< derived, n, const_unsafe_case< ret( arg ... ) >, more ... >
: wrapper_dispatch< derived, n, more ... > {
using wrapper_dispatch< derived, n, more ... >::operator ();
deprecated( "It is unsafe to call a std::function of non-const signature through a const access path." )
ret operator () ( arg ... a ) const
{ return ( (derived &) * this ) ( std::forward< arg >( a ) ... ); }
};
struct allocator_mismatch_error : std::exception // This should be implemented in a .cpp file, but stay header-only for now.
{ virtual char const * what() const noexcept override { return "An object could not be transferred into an incompatible memory allocation scheme."; } };
template< typename erasure_base, typename allocator >
typename std::enable_if< ! std::is_void< allocator >::value,
bool >::type nontrivial_target( erasure_base const * e, allocator const * ) {
std::type_info const * type = e->table->allocator_type;
if ( type == nullptr ) return false; // It's a raw pointer, PTMF, reference_wrapper, or nullptr.
if ( * type != typeid (allocator) ) throw allocator_mismatch_error{}; // Target belongs to a different allocation scheme.
return true;
}
inline bool nontrivial_target( void const *, void const * ) { return true; } // Give false positives when not checking the allocator.
template< typename ... free >
class wrapper_base {
template< typename, typename ... >
friend class wrapper;
typedef std::aligned_storage< sizeof (void *[3]) >::type effective_storage_type;
protected:
std::aligned_storage< sizeof (void *[4]), alignof(effective_storage_type) >::type storage;
void * storage_address() { return & storage; }
// init and destroy enter or recover from invalid states.
// They get on the right side of [basic.life]/7.4, but mind the exceptions.
// Default, move, and copy construction.
// Adopt by move.
template< typename allocator = void > // Allocator is already rebound to <char>.
void init_dirty( wrapper_base & s, allocator const * dest_alloc = nullptr ) {
decltype (erasure_utility::move_constructor_destructor) nontrivial;
if ( ! nontrivial_target( & s.erasure(), dest_alloc ) // No-op without an allocator. Save an access in pointer-like target case otherwise.
|| ! ( nontrivial = s.erasure().table->move_constructor_destructor ) ) {
std::memcpy( & this->erasure(), & s.erasure(), sizeof (storage) );
} else nontrivial( std::move( s.erasure() ), & this->erasure(), dest_alloc );
}
template< typename allocator = void > // Allocator is already rebound to <char>.
void init( wrapper_base && s, allocator const * dest_alloc = nullptr ) {
init_dirty( s, dest_alloc );
s.emplace_trivial( in_place_t< std::nullptr_t >{} );
}
// Adopt by copy.
template< typename allocator = void >
void init( wrapper_base const & s, allocator const * dest_alloc = nullptr ) {
decltype (erasure_utility::copy_constructor) nontrivial;
if ( ! nontrivial_target( & s.erasure(), dest_alloc ) // No-op without an allocator. Save an access in pointer-like target case otherwise.
|| ! ( nontrivial = s.erasure().table->copy_constructor ) ) {
std::memcpy( & this->erasure(), & s.erasure(), sizeof (storage) );
} else nontrivial( s.erasure(), & this->erasure(), dest_alloc );
}
template< typename source > // Target value is nullptr or a default-constructed pointer, which compares equal to nullptr.
void emplace_trivial( in_place_t< source >, source = {} ) noexcept
{ new (storage_address()) null_erasure( tag< free ... >{} ); }
// Pointers, PTMs, and reference wrappers bypass allocators completely.
template< typename t >
void emplace_trivial( in_place_t< t * >, t * p ) noexcept {
if ( p ) new (storage_address()) local_erasure< t * >( tag< std::false_type, free ... >{}, p );
else emplace_trivial( in_place_t< std::nullptr_t >{} );
}
template< typename t, typename c >
void emplace_trivial( in_place_t< t c::* >, t c::* ptm ) noexcept {
if ( ptm ) new (storage_address()) ptm_erasure< t c::* >( tag< free ... >{}, ptm );
else emplace_trivial( in_place_t< std::nullptr_t >{} );
}
template< typename t >
void emplace_trivial( in_place_t< std::reference_wrapper< t > >, std::reference_wrapper< t > r ) noexcept {
new (storage_address()) local_erasure< std::reference_wrapper< t > >( tag< std::false_type, free ... >{}, r );
}
void destroy() noexcept {
auto nontrivial = erasure().table->destructor;
if ( nontrivial ) nontrivial( erasure() );
}
// Implement erasure type verification for always-local targets without touching RTTI.
bool verify_type_impl( void * ) const noexcept
{ return erasure().table == & make_erasure_table< null_erasure, std::false_type, free ... >::value.utility; }
template< typename t >
bool verify_type_impl( std::reference_wrapper< t > * ) const noexcept {
return erasure().table
== & make_erasure_table< local_erasure< std::reference_wrapper< t > >, std::false_type, free ... >::value.utility;
}
template< typename t >
bool verify_type_impl( t ** ) const noexcept
{ return erasure().table == & make_erasure_table< local_erasure< t * >, std::false_type, free ... >::value.utility; }
template< typename t, typename c >
bool verify_type_impl( t c::** ) const noexcept
{ return erasure().table == & make_erasure_table< ptm_erasure< t c::* >, std::false_type, free ... >::value.utility; }
// User-defined class types are never guaranteed to be local. There could exist some allocator for which uses_allocator is true.
// RTTI could be replaced here by a small variable template linked from the table. Since we need it anyway, just use RTTI.
template< typename want >
bool verify_type_impl( want * ) const noexcept
{ return target_type() == typeid (want); }
public:
erasure_base & erasure()
{ return * launder_cast< erasure_base * >( & storage ); }
erasure_base const & erasure() const
{ return * launder_cast< erasure_base const * >( & storage ); }
void operator = ( wrapper_base && s ) noexcept { // Only suitable for generating implicit members in derived classes: wrong return type.
destroy();
init( std::move( s ) );
}
void operator = ( wrapper_base const & s ) {
wrapper_base temp;
temp.init( s );
destroy();
init( std::move( temp ) );
}
template< typename source >
typename std::enable_if< is_noexcept_erasable< source >::value >::type
operator = ( source const & s ) noexcept {
destroy();
emplace_trivial( in_place_t< source >{}, s );
}
template< std::size_t table_index, typename ret, typename ... arg >
ret call( arg && ... a ) const {
return get< table_index, ret( erasure_base const &, arg && ... ) >
( launder_cast< erasure_table< free ... > const * >( erasure().table )->dispatch )
( erasure(), std::forward< arg >( a ) ... );
}
std::type_info const & target_type() const noexcept
{ return erasure().table->target_type; }
template< typename want >
bool verify_type() const noexcept {
static_assert ( std::is_same< want, typename std::decay< want >::type >::value, "function target can only be of cv-unqualified object type." );
return verify_type_impl( (want *) nullptr );
}
template< typename want >
bool verify_type() const volatile noexcept
{ return const_cast< wrapper_base const * >( this )->verify_type< want >(); }
void const * complete_object_address() const noexcept
{ return erasure().table->target_access( erasure() ); }
void const volatile * complete_object_address() const volatile noexcept
{ return const_cast< wrapper_base const * >( this )->complete_object_address(); }
template< typename want >
want const * target() const noexcept {
if ( ! verify_type< want >() ) return nullptr;
return static_cast< want const * >( erasure().table->target_access( erasure() ) );
}
template< typename want >
want * target() noexcept
{ return const_cast< want * >( static_cast< wrapper_base const & >( * this ).target< want >() ); }
explicit operator bool () const volatile noexcept
{ return ! verify_type< void >(); }
void swap( wrapper_base & o ) noexcept { // Essentially same as std::swap, but skip some trivial destructors.
// Would be nice to use swap_ranges on trivially-copyable target storage. Double-check that launder can handle it.
wrapper_base temp; // This class is trivially constructed and destroyed.
temp.init_dirty( * this );
init_dirty( o );
o.init_dirty( temp );
}
};
template< typename t >
typename std::remove_cv< t >::type value( t & v ) { return v; }
template< typename t >
t value( t const && v ) { return std::move( v ); }
template< typename t >
t && value( t && v ) { return std::move( v ); }
template< typename target, typename ... free >
typename std::enable_if< conjunction<
std::is_same< target, typename target::UGLY(wrapper_type) >
, std::is_base_of< wrapper_base< free ... >, target > >::value,
bool >::type is_empty_wrapper( target *, wrapper_base< free ... > const * arg )
{ return ! * arg; }
inline bool is_empty_wrapper( ... ) { return false; }
template< bool is_compatibly_wrapped, typename target, typename allocator >
struct rebind_allocator_for_source
{ typedef typename std::allocator_traits< allocator >::template rebind_alloc< target > type; };
template< typename target, typename allocator >
struct rebind_allocator_for_source< true, target, allocator >
{ typedef common_allocator_rebind< allocator > type; };
template< typename derived, bool >
struct nullptr_construction {
nullptr_construction() noexcept
{ static_cast< derived * >( this )->emplace_trivial( in_place_t< std::nullptr_t >{} ); }
nullptr_construction( tag< struct trivialize > ) {}
};
template< typename derived >
struct nullptr_construction< derived, false > {
nullptr_construction() noexcept = delete;
nullptr_construction( tag< struct trivialize > ) {}
};
template< typename target_policy, typename ... sig >
class wrapper
: public wrapper_base< typename member_to_free< sig >::type ... >
, private nullptr_construction< wrapper< target_policy, sig ... >, target_policy::template temp< std::nullptr_t >::value >
, public wrapper_dispatch< wrapper< target_policy, sig ... >, 0, sig ... > {
using wrapper::wrapper_base::storage;
using wrapper::wrapper_base::storage_address;
using wrapper::wrapper_base::init;
using wrapper::wrapper_base::emplace_trivial;
protected: // Queries on potential targets. Shared with container_wrapper.
template< typename target >
using is_targetable = typename target_policy::template temp< target >;
template< typename source >
struct is_small {
static const bool value = sizeof (local_erasure< source >) <= sizeof (storage)
&& alignof (decltype (storage)) % alignof (source) == 0
&& std::is_nothrow_move_constructible< source >::value;
};
template< typename source, typename = source, typename = typename wrapper::wrapper_base >
struct is_compatibly_wrapped : std::false_type {};
template< typename source >
struct is_compatibly_wrapped< source, typename source::UGLY(wrapper_type), typename source::wrapper_base >
: std::true_type {};
template< typename source, typename allocator >
bool nontrivial_target( source const & s, allocator const * alloc )
{ return impl::nontrivial_target( & s.erasure(), alloc ); } // Use friend relationship with compatible wrappers.
private:
template< typename target, typename allocator, typename ... arg >
void allocate( allocator * alloc, arg && ... a ) {
if ( is_empty_wrapper( (target *) nullptr, & a ... ) ) {
return emplace_trivial( in_place_t< std::nullptr_t >{} );
}
typedef allocator_erasure< allocator, target > erasure;
// TODO: Add a new erasure template to put the fancy pointer on the heap.
static_assert ( sizeof (erasure) <= sizeof storage, "Stateful allocator or fancy pointer is too big for polymorphic function wrapper." );
new (storage_address()) erasure( tag< typename target_policy::copies, sig ... >{}, std::allocator_arg, * alloc, std::forward< arg >( a ) ... );
}
// When value-wise initialization isn't adoption, delegate to in-place construction or allocation.
template< typename source >
typename std::enable_if< ! is_compatibly_wrapped< typename std::decay< source >::type >::value >::type
init( source && s )
{ emplace< typename std::decay< source >::type >( std::forward< source >( s ) ); }
template< typename source, typename allocator >
typename std::enable_if< ! is_compatibly_wrapped< typename std::decay< source >::type >::value >::type
init( source && s, allocator * a )
{ allocate< typename std::decay< source >::type >( a, std::forward< source >( s ) ); }
template< typename source, typename ... arg >
typename std::enable_if< is_small< source >::value && ! is_noexcept_erasable< source >::value >::type
emplace( arg && ... a )
{ new (storage_address()) local_erasure< source >( tag< typename target_policy::copies, sig ... >{}, std::forward< arg >( a ) ... ); }
template< typename source, typename ... arg >
typename std::enable_if< is_noexcept_erasable< source >::value >::type
emplace( arg && ... a )
{ emplace_trivial( in_place_t< source >{}, std::forward< arg >( a ) ... ); }
template< typename target, typename ... arg >
typename std::enable_if< ! is_small< target >::value >::type
emplace( arg && ... a ) {
std::allocator< target > alloc;
allocate< target >( & alloc, std::forward< arg >( a ) ... );
}
public:
typedef wrapper UGLY(wrapper_type);
wrapper() noexcept = default;
friend typename wrapper::nullptr_construction;
#define NON_NULLPTR_CONSTRUCT : wrapper::nullptr_construction( tag< trivialize >{} )
wrapper( wrapper && s ) noexcept NON_NULLPTR_CONSTRUCT
{ init( std::move( s ) ); }
wrapper( wrapper const & s ) NON_NULLPTR_CONSTRUCT
{ init( s ); }
MSVC_SKIP ( // MSVC cannot parse deprecated constructor templates, so remove this entirely.
template< typename allocator >
deprecated( "This constructor ignores its allocator argument. Specify allocation per-target or use function_container instead." )
wrapper( std::allocator_arg_t, allocator const & ) NON_NULLPTR_CONSTRUCT
{ init( in_place_t< std::nullptr_t >{}, nullptr ); }
)
template< typename source,
typename std::enable_if< conjunction<
negation< std::is_base_of< wrapper, typename std::decay< source >::type > >
, is_targetable< typename std::decay< source >::type >
>::value >::type * = nullptr >
wrapper( source && s )
noexcept( is_noexcept_erasable< typename std::decay< source >::type >::value
|| is_compatibly_wrapped< source >::value ) NON_NULLPTR_CONSTRUCT
{ init( std::forward< source >( s ) ); }
template< typename allocator, typename source,
typename = typename std::enable_if<
is_targetable< typename std::decay< source >::type >::value
>::type >
wrapper( std::allocator_arg_t, allocator && alloc, source && s )
noexcept( is_noexcept_erasable< typename std::decay< source >::type >::value ) NON_NULLPTR_CONSTRUCT {
// Adapt the allocator to the source. If it's already an rvalue of the right type, use it in-place.
// It might make more sense to use modifiable lvalues in-place, but this is closer to the standard.
typename rebind_allocator_for_source< is_compatibly_wrapped< typename std::decay< source >::type >::value,
typename std::decay< source >::type, typename std::decay< allocator >::type
>::type && target_alloc = value( std::forward< allocator >( alloc ) );
init( std::forward< source >( s ), & target_alloc );
}
template< typename target, typename ... arg,
typename = typename std::enable_if< is_targetable< target >::value >::type >
wrapper( in_place_t< target >, arg && ... a )
noexcept( is_noexcept_erasable< target >::value )
{ emplace< target >( std::forward< arg >( a ) ... ); }
template< typename allocator, typename target, typename ... arg,
typename = typename std::enable_if<
is_targetable< target >::value
>::type >
wrapper( std::allocator_arg_t, allocator && alloc, in_place_t< target >, arg && ... a )
noexcept( is_noexcept_erasable< target >::value ) {
typename std::allocator_traits< typename std::decay< allocator >::type >::template rebind_alloc< target >
&& target_alloc = value( std::forward< allocator >( alloc ) );
allocate< target >( & target_alloc, std::forward< arg >( a ) ... );
}
#undef NON_NULLPTR_CONSTRUCT
wrapper & operator = ( wrapper && ) = default;
wrapper & operator = ( wrapper const & ) = default;
template< typename source >
typename std::enable_if<
is_noexcept_erasable< typename std::decay< source >::type >::value
|| is_compatibly_wrapped< typename std::decay< source >::type >::value
>::type operator = ( source && s )
noexcept( is_noexcept_erasable< typename std::decay< source >::type >::value || is_compatibly_wrapped< source >::value )
{ wrapper::wrapper_base::operator = ( std::forward< source >( s ) ); }
template< typename source >
typename std::enable_if<
! is_noexcept_erasable< typename std::decay< source >::type >::value
&& ! is_compatibly_wrapped< typename std::decay< source >::type >::value
>::type operator = ( source && s )
{ wrapper::wrapper_base::operator = ( wrapper( std::forward< source >( s ) ) ); }
template< typename source, typename allocator >
void assign( source && s, allocator const & alloc )
noexcept( is_noexcept_erasable< typename std::decay< source >::type >::value )
{ * this = wrapper( std::allocator_arg, alloc, std::forward< source >( s ) ); }
// Quirk: If emplacing constructor throws, but move constructor does not, and the target is small, it may be moved for the sake of strong exception safety.
template< typename source, typename ... arg >
void emplace_assign( arg && ... a ) // TODO separate non-container case avoiding saved_allocator.
noexcept( is_noexcept_erasable< source >::value )
{ * this = wrapper( in_place_t< source >{}, std::forward< arg >( a ) ... ); }
template< typename source, typename allocator, typename ... arg >
void allocate_assign( allocator const & alloc, arg && ... a )
noexcept( is_noexcept_erasable< source >::value )
{ * this = wrapper( std::allocator_arg, alloc, in_place_t< source >{}, std::forward< arg >( a ) ... ); }
~ wrapper() noexcept
{ this->destroy(); }
};
template< typename saved_allocator, typename used_allocator >
void assign_allocator( std::true_type, saved_allocator * d, used_allocator * s )
{ * d = std::move( * s ); }
inline void assign_allocator( std::false_type, void *, void const * ) {}
template< typename allocator >
void allocator_swap( std::true_type, allocator * lhs, allocator * rhs )
{ std::iter_swap( lhs, rhs ); }