-
Notifications
You must be signed in to change notification settings - Fork 13
/
cxx_function_msvc.hpp
1188 lines (1019 loc) · 56.4 KB
/
cxx_function_msvc.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 <tuple>
#include <type_traits>
#include <utility>
#ifdef _MSC_VER
# include <scoped_allocator> // for a workaround, that scoped_allocator_adaptor is not Callable.
#endif
namespace cxx_function {
// Dispatch tag for in-place construction, for when explicit template arguments are unavailable (e.g. constructor calls).
template< typename >
struct in_place_t {};
#if __cplusplus >= 201402
template< typename t >
constexpr in_place_t< t > in_place = {};
#endif
namespace impl {
#ifdef _MSC_VER
template< typename t >
using id_t = t; // Help parser put decltype-id in a nested-name-specifier.
typedef std::allocator< char > stdallocchar;
# define allocator cxx_function_allocator // MSVC name lookup is horribly broken.
# define alloc cxx_function_alloc // Any template parameter names that might collide with a member of any namespace should go here.
# 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, QUALS ) MACRO( QUALS, UNSAFE ) MACRO( const QUALS, IGNORE )
#define DISPATCH_CV( MACRO, UNSAFE, QUALS ) DISPATCH_CQ( MACRO, UNSAFE, QUALS ) DISPATCH_CQ( MACRO, IGNORE, volatile QUALS )
// Apply a given macro over all reference qualifications.
#define DISPATCH_CVREFQ( MACRO, QUALS ) DISPATCH_CV( MACRO, IGNORE, & QUALS ) DISPATCH_CV( MACRO, IGNORE, && QUALS )
// Apply a given macro over all type qualifications.
#define DISPATCH_ALL( MACRO ) DISPATCH_CV( MACRO, UNPACK, ) DISPATCH_CVREFQ( MACRO, )
// Convert a member function signature to its free invocation counterpart.
template< typename sig >
struct implicit_object_to_parameter;
template< typename t >
struct add_reference
{ typedef t & type; };
template< typename t >
struct add_reference< t && >
{ typedef t && type; };
struct erasure_handle {}; // The member function should belong to a class derived from this base.
#define TYPE_CONVERT_CASE( QUALS, UNSAFE ) \
template< typename ret, typename ... arg > \
struct implicit_object_to_parameter< ret( arg ... ) QUALS > \
{ typedef ret type( add_reference< erasure_handle QUALS >::type, arg ... ); };
DISPATCH_ALL( TYPE_CONVERT_CASE )
#undef TYPE_CONVERT_CASE
// Apply given cv-qualifiers and reference category to a new type.
template< typename source, typename target >
struct transfer_qualifiers;
#define TRANSFER_QUALS_CASE( QUALS, UNSAFE ) \
template< typename source, typename target > \
struct transfer_qualifiers< source QUALS, target > \
{ typedef target QUALS type; };
DISPATCH_CVREFQ( TRANSFER_QUALS_CASE, )
#undef TRANSFER_QUALS_CASE
/* 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 a std::tuple of function pointers and type_info*'s.
Entries that would be trivial or useless may be set to nullptr.
*/
enum class dispatch_slot {
destructor,
move_constructor_destructor,
copy_constructor,
target_access,
target_type,
allocator_type,
base_index
};
constexpr int operator + ( dispatch_slot e ) { return static_cast< int >( e ); }
// "Abstract" base class for the island inside the wrapper class, e.g. std::function.
// This must appear first in the most-derived class layout.
template< typename ... free >
struct erasure_base : erasure_handle {
typedef std::tuple<
void (*)( erasure_handle &, void * alloc ), // destructor
void (*)( erasure_handle &&, void * dest, void * source_alloc, void * dest_alloc ), // move constructor + destructor
void (*)( erasure_handle const &, void * dest, void * alloc ), // copy constructor
void const * (*)( erasure_handle const & ), // target access
std::type_info const &, // target_type
std::type_info const *, // allocator_type
free * ... // dispatchers
> dispatch_table;
dispatch_table const & table;
constexpr erasure_base( dispatch_table const & in_table )
: table( in_table ) {}
};
// Generic "virtual" functions to manage the wrapper payload lifetime.
template< typename derived >
struct erasure_special {
static void destroy( erasure_handle & self, void * ) noexcept
{ static_cast< derived & >( self ). ~ derived(); }
static void move( erasure_handle && self, void * dest, void *, void * ) {
new (dest) derived( std::move( static_cast< derived & >( self ) ) );
destroy( self, {} );
}
static void copy( erasure_handle const & self, void * dest, void * )
{ 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.
// Most are specialized for allocator_erasure.
template< typename >
struct is_allocator_erasure : std::false_type {};
template< typename derived >
constexpr typename std::enable_if<
! std::is_trivially_destructible< derived >::value
|| is_allocator_erasure< derived >::value >::type
( * erasure_destroy() ) ( erasure_handle &, void * )
{ return & derived::destroy; }
template< typename derived >
constexpr typename std::enable_if<
std::is_trivially_destructible< derived >::value
&& ! is_allocator_erasure< derived >::value >::type
( * erasure_destroy() ) ( erasure_handle &, void * )
{ return nullptr; }
template< typename erasure >
struct erasure_trivially_movable : std::integral_constant< bool,
std::is_trivially_constructible< erasure, 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_handle &&, void *, void *, void * )
{ return & derived::move; }
template< typename derived >
constexpr typename std::enable_if< erasure_trivially_movable< derived >::value >::type
( * erasure_move() ) ( erasure_handle &&, void *, void *, void * )
{ 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 >
constexpr typename std::enable_if< erasure_nontrivially_copyable< derived >::value >::type
( * erasure_copy() ) ( erasure_handle const &, void *, void * )
{ return & derived::copy; }
template< typename derived >
constexpr typename std::enable_if< ! erasure_nontrivially_copyable< derived >::value >::type
( * erasure_copy() ) ( erasure_handle const &, void *, void * )
{ return nullptr; }
template< typename derived >
constexpr typename std::enable_if< is_allocator_erasure< derived >::value,
std::type_info const * >::type erasure_allocator_type()
{ return & typeid (typename derived::common_allocator); }
template< typename derived >
constexpr typename std::enable_if< ! is_allocator_erasure< derived >::value,
std::type_info const * >::type erasure_allocator_type()
{ return nullptr; }
template< typename >
struct const_unsafe_case; // internal tag for function signatures introduced for backward compatibility of const-qualified access
template< typename erasure, typename free >
struct erasure_dispatch;
#define DISPATCH_TABLE( NAME, TARGET_TYPE, TPARAM, TARG, ... ) \
/* Generate dispatcher function. */ \
template< UNPACK TPARAM, typename ret, typename erasure_base_ref, typename ... arg > \
struct erasure_dispatch< NAME< UNPACK TARG >, ret( erasure_base_ref, arg ... ) > { \
static ret call( erasure_base_ref self_base, arg ... a ) { \
typedef typename transfer_qualifiers< erasure_base_ref, NAME< UNPACK TARG > >::type erasure_ref; \
auto && self = static_cast< erasure_ref >( self_base ); \
__VA_ARGS__ \
} \
}; \
/* Generate "vtable". */ \
template< UNPACK TPARAM > \
typename erasure_base< sig ... >::dispatch_table const NAME< UNPACK TARG >::table = typename NAME::dispatch_table{ \
erasure_destroy< NAME >(), \
erasure_move< NAME >(), \
erasure_copy< NAME >(), \
& NAME::target_access, \
typeid (TARGET_TYPE), \
erasure_allocator_type< NAME >(), \
& erasure_dispatch< NAME, sig >::call ... \
};
// Implement the uninitialized state.
template< typename ... sig >
struct null_erasure
: erasure_base< sig ... > // "vtable" interface class
, erasure_special< null_erasure< sig ... > > { // generic implementations of "virtual" functions
static const typename erasure_base< sig ... >::dispatch_table table; // own "vtable"
// 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_handle const & ) { return nullptr; } // target<void>() still returns nullptr.
null_erasure() noexcept
: null_erasure::erasure_base( table ) {} // Initialize own "vtable pointer" at runtime.
};
DISPATCH_TABLE( null_erasure, void, ( typename ... sig ), ( sig ... ),
(void) self; (void) sizeof ... (a); throw std::bad_function_call{};
)
// Implement erasures of objects which are small and have a well-defined call operator.
template< typename target_type, typename ... sig >
struct local_erasure
: erasure_base< sig ... >
, erasure_special< local_erasure< target_type, sig ... > > {
static const typename erasure_base< sig ... >::dispatch_table table;
target_type target;
static void const * target_access( erasure_handle const & self )
{ return & static_cast< local_erasure const & >( self ).target; }
template< typename ... arg >
local_erasure( arg && ... a )
: local_erasure::erasure_base( table )
, target( std::forward< arg >( a ) ... ) {}
};
DISPATCH_TABLE( local_erasure, target_type, ( typename target_type, typename ... sig ), ( target_type, sig ... ),
// Directly call the name "target," not a reference, to support devirtualization.
return std::forward< erasure_ref >( self ).target( std::forward< arg >( a ) ... );
)
// Implement erasures of pointer-to-members, which need std::mem_fn instead of a direct call.
template< typename target_type, typename ... sig >
struct ptm_erasure
: erasure_base< sig ... >
, erasure_special< ptm_erasure< target_type, sig ... > > {
static const typename erasure_base< sig ... >::dispatch_table table;
target_type target; // Do not use mem_fn here...
static void const * target_access( erasure_handle const & self )
{ return & static_cast< ptm_erasure const & >( self ).target; } // ... because the user can get read/write access to the target object.
ptm_erasure( target_type a )
: ptm_erasure::erasure_base( table )
, target( a ) {}
};
DISPATCH_TABLE( ptm_erasure, target_type, ( typename target_type, typename ... sig ), ( target_type, sig ... ),
return std::mem_fn( 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, typename = void >
struct is_always_equal : std::false_type {};
template< typename t >
struct is_always_equal< t, void, typename std::enable_if< std::allocator_traits< t >::is_always_equal::value >::type >
: std::true_type {};
template< typename t, typename v >
struct is_always_equal< t, v, typename std::enable_if< MSVC_FIX(id_t<) decltype (std::declval< t >() == std::declval< t >()) MSVC_FIX(>)::value >::type >
: std::true_type {};
MSVC_SKIP ( // MSVC implements is_always_equal and fails to see <allocator<t>,void,void> as more specialized than <t,void,void>.
template< typename t >
struct is_always_equal< std::allocator< t > >
: std::true_type {};
)
template< typename allocator, typename target_type, typename ... sig >
struct allocator_erasure
: erasure_base< sig ... >
, allocator { // empty base class optimization (EBCO)
typedef std::allocator_traits< allocator > allocator_traits;
typedef common_allocator_rebind< allocator > common_allocator;
static const typename erasure_base< sig ... >::dispatch_table table;
typename allocator_traits::pointer target;
allocator & alloc() { return static_cast< allocator & >( * this ); }
allocator const & alloc() const { return static_cast< allocator const & >( * this ); }
target_type * target_address() { return std::addressof( * target ); }
static void const * target_access( erasure_handle 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;
allocator_erasure( allocator_erasure const & ) = delete;
template< typename ... arg >
allocator_erasure( std::allocator_arg_t, allocator const & in_alloc, arg && ... a )
: allocator_erasure::erasure_base( table )
, allocator( in_alloc )
, target( allocator_traits::allocate( alloc(), 1 ) )
{ construct_safely( std::forward< arg >( a ) ... ); }
// Move-construct into a different pool.
allocator_erasure( std::allocator_arg_t, allocator const & dest_allocator, allocator_erasure && o )
: allocator_erasure( std::allocator_arg, dest_allocator, std::move( * o.target ) ) {}
// Common case: copy-construct with any allocator, same or different.
allocator_erasure( std::allocator_arg_t, allocator const & dest_allocator, allocator_erasure const & o )
: allocator_erasure( std::allocator_arg, dest_allocator, * o.target ) {}
void move( std::true_type, void * dest, void *, void * ) 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 * source_allocator_v, void * dest_allocator_v ) {
auto * dest_allocator_p = static_cast< common_allocator * >( 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, source_allocator_v, dest_allocator_v ); // same pool
} else { // different pool
auto & e = * new (dest) allocator_erasure( std::allocator_arg, static_cast< allocator >( * dest_allocator_p ), // Reallocate.
std::move_if_noexcept( * this ) ); // Protect user against their own throwing move constructors.
* dest_allocator_p = e.alloc(); // Update the wrapper allocator instance with the new copy, potentially updated by the new allocation.
destroy( * this, source_allocator_v );
}
}
// [*_]allocator_v points to the wrapper allocator instance, if any.
static void move( erasure_handle && self_base, void * dest, void * source_allocator_v, void * 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 >{}, dest, source_allocator_v, dest_allocator_v );
}
static void copy( erasure_handle const & self_base, void * dest, void * dest_allocator_v ) {
auto * dest_allocator_p = static_cast< common_allocator * >( dest_allocator_v );
auto & self = static_cast< allocator_erasure const & >( self_base );
// Structure the control flow differently to avoid instantiating the copy constructor.
allocator const & dest_allocator = dest_allocator_p?
static_cast< allocator const & >( * dest_allocator_p ) : self.alloc();
auto & e = * new (dest) allocator_erasure( std::allocator_arg, dest_allocator, self );
if ( dest_allocator_p ) * dest_allocator_p = static_cast< common_allocator const & >( e.alloc() ); // Likewise, update the wrapper allocator instance with the new copy.
}
static void destroy( erasure_handle & self_base, void * allocator_v ) 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 );
if ( allocator_v ) * static_cast< common_allocator * >( allocator_v ) = static_cast< common_allocator const & >( self.alloc() );
self. ~ allocator_erasure();
}
};
template< typename allocator, typename target_type, typename ... sig >
struct is_allocator_erasure< allocator_erasure< allocator, target_type, sig ... > > : std::true_type {};
template< typename allocator, typename target_type, typename ... sig >
struct erasure_trivially_movable< allocator_erasure< allocator, target_type, sig ... > > : std::integral_constant< bool,
std::is_trivially_constructible< allocator_erasure< allocator, target_type, sig ... >, allocator_erasure< allocator, target_type, sig ... > && >::value
&& std::is_trivially_destructible< allocator_erasure< allocator, target_type, sig ... > >::value
&& is_always_equal< allocator >::value > {};
template< typename allocator, typename target_type, typename ... sig >
struct erasure_nontrivially_copyable< allocator_erasure< allocator, target_type, sig ... > >
: std::is_copy_constructible< target_type > {};
DISPATCH_TABLE( allocator_erasure, target_type, ( typename allocator, typename target_type, typename ... sig ), ( allocator, target_type, sig ... ),
return std::forward< typename transfer_qualifiers< erasure_ref, target_type >::type >( * self.target )( std::forward< arg >( a ) ... );
)
// Metaprogramming for checking a potential target against a list of signatures.
template< bool ... cond >
struct logical_intersection
: std::true_type {};
template< bool ... cond >
struct logical_intersection< true, cond ... >
: logical_intersection< cond ... >::type {};
template< bool ... cond >
struct logical_intersection< false, cond ... >
: std::false_type {};
#ifdef _MSC_VER
# define IS_CALLABLE_DISPATCH is_callable_dispatch
#else
# define IS_CALLABLE_DISPATCH is_callable
#endif
template< typename t, typename sig, typename = void >
struct IS_CALLABLE_DISPATCH : std::false_type {};
#define IS_CALLABLE_CASE( QUALS, UNSAFE ) \
template< typename t, typename ret, typename ... arg > \
struct IS_CALLABLE_DISPATCH< t, ret( arg ... ) QUALS, \
typename std::enable_if< std::is_convertible< \
typename std::result_of< typename add_reference< t QUALS >::type ( arg ... ) >::type \
, ret >::value >::type > \
: std::true_type {};
DISPATCH_ALL( IS_CALLABLE_CASE )
#undef IS_CALLABLE_CASE
MSVC_FIX (
template< typename t, typename sig >
struct is_callable : is_callable_dispatch< t, sig > {};
template< typename allocator, typename sig >
struct is_callable< std::scoped_allocator_adaptor< allocator >, sig >
: std::false_type {};
template< typename t, typename sig >
struct is_callable< in_place_t< t >, sig >
: std::false_type {};
)
template< typename sig >
struct is_callable< std::nullptr_t, sig >
: std::true_type {};
template< typename ... sig >
struct is_all_callable {
template< typename t >
using temp = typename logical_intersection< is_callable< t, sig >::value ... >::type;
};
template< typename self, typename ... sig >
struct is_copyable_all_callable {
template< typename t, typename = void >
struct temp : std::integral_constant< bool,
std::is_copy_constructible< t >::value
&& is_all_callable< sig ... >::template temp< t >::value > {};
template< typename v > // Presume that self is a copyable wrapper, since that is what uses this metafunction.
struct temp< self, v > : std::true_type {};
};
// 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.
};
// 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( \
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 ... ) 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 ) QUALS { \
auto && self = static_cast< typename add_reference< derived QUALS >::type >( * this ); \
return std::get< + dispatch_slot::base_index + table_index >( self.erasure().table ) \
( std::forward< decltype (self) >( self ).erasure(), 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 const_cast< derived & >( static_cast< derived const & >( * this ) )
( std::forward< arg >( a ) ... );
}
};
template< typename ... sig >
class wrapper_base
: public wrapper_dispatch< wrapper_base< sig ... >, 0, sig ... > {
template< template< typename ... > class, 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.
void init( in_place_t< std::nullptr_t >, std::nullptr_t ) noexcept
{ new (storage_address()) null_erasure< typename implicit_object_to_parameter< sig >::type ... >; }
// Pointers are local callables.
template< typename t >
void init( in_place_t< t * >, t * p ) noexcept {
if ( p ) new (storage_address()) local_erasure< t *, typename implicit_object_to_parameter< sig >::type ... >( p );
else init( in_place_t< std::nullptr_t >{}, nullptr );
}
// PTMs are like local callables.
template< typename t, typename c >
void init( in_place_t< t c::* >, t c::* ptm ) noexcept {
if ( ptm ) new (storage_address()) ptm_erasure< t c::*, typename implicit_object_to_parameter< sig >::type ... >( ptm );
else init( in_place_t< std::nullptr_t >{}, nullptr );
}
// Implement erasure type verification for always-local targets without touching RTTI.
bool verify_type_impl( void * ) const noexcept
{ return & erasure().table == & null_erasure< typename implicit_object_to_parameter< sig >::type ... >::table; }
template< typename t >
bool verify_type_impl( std::reference_wrapper< t > * ) const noexcept
{ return & erasure().table == & local_erasure< std::reference_wrapper< t >, typename implicit_object_to_parameter< sig >::type ... >::table; }
template< typename t >
bool verify_type_impl( t ** ) const noexcept
{ return & erasure().table == & local_erasure< t *, typename implicit_object_to_parameter< sig >::type ... >::table; }
template< typename t, typename c >
bool verify_type_impl( t c::** ) const noexcept
{ return & erasure().table == & ptm_erasure< t c::*, typename implicit_object_to_parameter< sig >::type ... >::table; }
// 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:
MSVC_FIX( using wrapper_dispatch::operator(); ) // MSVC has trouble in derived classes with indirect inheritance.
#define ERASURE_ACCESS( QUALS, UNSAFE ) \
erasure_base< typename implicit_object_to_parameter< sig >::type ... > QUALS erasure() QUALS \
{ return reinterpret_cast< erasure_base< typename implicit_object_to_parameter< sig >::type ... > QUALS >( storage ); }
DISPATCH_CVREFQ( ERASURE_ACCESS, )
#undef ERASURE_ACCESS
std::type_info const & target_type() const noexcept
{ return std::get< + dispatch_slot::target_type >( erasure().table ); }
template< typename want >
bool verify_type() const noexcept {
static_assert ( ! std::is_reference< want >::value, "function does not support reference-type targets." );
static_assert ( ! std::is_const< want >::value && ! std::is_volatile< want >::value, "function does not support cv-qualified targets." );
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 std::get< + dispatch_slot::target_access >( erasure().table ) ( 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 * >( std::get< + dispatch_slot::target_access >( erasure().table ) ( 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 >(); }
};
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."; } };
// Mix-in a persistent allocator to produce a [unique_]function_container.
template< typename allocator >
class wrapper_allocator
: common_allocator_rebind< allocator > {
typedef std::allocator_traits< common_allocator_rebind< allocator > > allocator_traits;
void do_swap( std::true_type, wrapper_allocator & o ) noexcept
{ using std::swap; swap( actual_allocator(), o.actual_allocator() ); }
void do_swap( std::false_type, wrapper_allocator & o ) noexcept
{ assert ( actual_allocator() == o.actual_allocator() && "Cannot swap containers while not-swapping their unequal allocators." ); } // Roughly match libc++.
protected:
wrapper_allocator( wrapper_allocator && ) /*noexcept*/ = default;
/* Do select_on_container_copy_construction here, although the "container" is really represented
by the set of all equivalent allocators in various independent target objects. */
wrapper_allocator( wrapper_allocator const & o )
: common_allocator_rebind< allocator >( allocator_traits::select_on_container_copy_construction( o ) )
{}
// Likewise the rationale for POCMA, POCCA, and POCS. They propagate freely between targets, not between containers.
wrapper_allocator & operator = ( wrapper_allocator && o ) noexcept {
if ( typename allocator_traits::propagate_on_container_move_assignment() ) actual_allocator() = std::move( o.actual_allocator() );
return * this;
}
wrapper_allocator & operator = ( wrapper_allocator const & o ) noexcept {
if ( typename allocator_traits::propagate_on_container_copy_assignment() ) actual_allocator() = o;
return * this;
}
void swap( wrapper_allocator & o ) noexcept
{ do_swap( allocator_traits::propagate_on_container_swap(), o ); }
static constexpr bool noexcept_move_assign = allocator_traits::propagate_on_container_move_assignment::value
|| MSVC_FIX(impl::)is_always_equal< allocator >::value;
static constexpr bool noexcept_move_adopt = false; // Adoption may produce an allocator_mismatch_error.
template< typename derived, typename t >
derived reallocate( t && o )
{ return { std::allocator_arg, actual_allocator(), std::forward< t >( o ) }; }
common_allocator_rebind< allocator > & actual_allocator()
{ return * this; }
// Determine whether to use the stored allocator for an initialization or assignment.
template< typename erasure_base > // Templating this is a bit bogus, since it only uses the "vtable header," but the type system doesn't know that.
common_allocator_rebind< allocator > * compatible_allocator( erasure_base const & e ) {
std::type_info const * type = std::get< + dispatch_slot::allocator_type >( e.table ); // Get dynamic type of the source allocator.
if ( type == nullptr ) return nullptr; // It's a local erasure. Allocators don't apply. Avoid the potentially expensive type_info::operator==.
if ( * type != typeid (actual_allocator()) ) throw allocator_mismatch_error{}; // Oh no!
return & actual_allocator();
}
common_allocator_rebind< allocator > * any_allocator()
{ return & actual_allocator(); }
typedef allocator allocator_t;
public: // Include the container-like interface.
wrapper_allocator() = default;
wrapper_allocator( std::allocator_arg_t, allocator const & in_alloc ) noexcept
: common_allocator_rebind< allocator >( in_alloc ) {}
allocator get_allocator() const
{ return * this; }
};
// Stub-out the container support.
class wrapper_no_allocator {
protected:
static constexpr bool noexcept_move_assign = true;
static constexpr bool noexcept_move_adopt = true;
template< typename, typename t >
t && reallocate( t && o )
{ return std::forward< t >( o ); }
template< typename derived, typename t >
derived reallocate( t const & o )
{ return o; }
// This defines the default allocation of targets generated by non-container functions.
typedef wrapper_no_allocator allocator_t;
MSVC_SKIP(std::allocator< char >) MSVC_FIX(stdallocchar) actual_allocator() { return {}; }
template< typename erasure_base >
static constexpr void * compatible_allocator( erasure_base const & )
{ return nullptr; }
static constexpr void * any_allocator()
{ return nullptr; }
wrapper_no_allocator() = default;
// This is used internally but not publicly accessible. (It's publicly *visible* because inheriting constructors don't work very well.)
template< typename ignored >
wrapper_no_allocator( std::allocator_arg_t, ignored const & ) noexcept {}
void swap( wrapper_no_allocator & ) {}
};
template< template< typename ... > class is_targetable, typename allocator_manager, typename ... sig >
class wrapper
: public allocator_manager
, MSVC_FIX(protected) wrapper_base< sig ... > {
template< template< typename ... > class, typename, typename ... >
friend class wrapper;
MSVC_SKIP ( // MSVC can't resolve an inherited injected-class-name nested-name-specifier, and doesn't need these.
using wrapper::wrapper_base::erasure;
using wrapper::wrapper_base::storage;
using wrapper::wrapper_base::storage_address;
)
using MSVC_SKIP(wrapper::)wrapper_base::init;
// Queries on potential targets.
template< typename source, typename allocator = typename allocator_manager::allocator_t >
struct is_small {
static const bool value = sizeof (local_erasure< source, typename implicit_object_to_parameter< sig >::type ... >)
<= sizeof (MSVC_FIX(wrapper_base::)storage)
&& alignof (source) <= alignof (decltype (MSVC_FIX(wrapper_base::)storage))
&& ! std::uses_allocator< source, allocator >::value
&& std::is_nothrow_move_constructible< source >::value;
};
template< typename allocator >
struct is_small< std::nullptr_t, allocator >
: std::true_type {};
template< typename, typename = MSVC_SKIP(typename wrapper::)wrapper_base >
struct is_compatibly_wrapped : std::false_type
{ static const bool with_compatible_allocation = false; };
template< typename source >
struct is_compatibly_wrapped< source, typename source::wrapper_base >
: std::true_type {
static const bool with_compatible_allocation = allocator_manager::noexcept_move_adopt
|| std::is_same< MSVC_SKIP(typename wrapper::)allocator_t, typename source::wrapper::allocator_t >::value;
};
// Adopt by move.
template< typename source >
typename std::enable_if< is_compatibly_wrapped< source >::value >::type
init( in_place_t< source >, source && s ) {
typename source::wrapper & o = s;
auto nontrivial = std::get< + dispatch_slot::move_constructor_destructor >( o.erasure().table );
if ( ! nontrivial ) {
std::memcpy( & erasure(), & o.erasure(), sizeof (storage) );
} else {
nontrivial( std::move( o ).erasure(), storage_address(), o.any_allocator(), allocator_manager::compatible_allocator( o.erasure() ) );
}
o.init( in_place_t< std::nullptr_t >{}, nullptr );
}
// Adopt by copy.
template< typename source >
typename std::enable_if< is_compatibly_wrapped< source >::value >::type
init( in_place_t< source >, source const & s ) {
typename wrapper::wrapper_base const & o = s;
auto nontrivial = std::get< + dispatch_slot::copy_constructor >( o.erasure().table );
if ( ! nontrivial ) std::memcpy( & erasure(), & o.storage, sizeof (storage) );
else nontrivial( o.erasure(), storage_address(), allocator_manager::compatible_allocator( o.erasure() ) );
}
// In-place construction of a compatible source uses a temporary to check its constraints.
template< typename source, typename ... arg >
typename std::enable_if< is_compatibly_wrapped< source >::value && ! std::is_same< source, wrapper >::value >::type
init( in_place_t< source > t, arg && ... a )
{ init( t, source( std::forward< arg >( a ) ... ) ); }
// Discard an allocator argument that is unused or already permanently retained.
template< typename allocator, typename source, typename ... arg >
typename std::enable_if< is_small< source >::value
|| ( is_compatibly_wrapped< source >::value && std::is_same< allocator, typename allocator_manager::allocator_t >::value ) >::type
init( std::allocator_arg_t, allocator const &, in_place_t< source > t, arg && ... a )
{ init( t, std::forward< arg >( a ) ... ); }
// Otherwise, given an allocator and constructor arguments for obtaining a compatible source, normalize allocation by introducing a temporary container object.
template< typename allocator, typename source, typename ... arg >
typename std::enable_if< is_compatibly_wrapped< source >::value && ! std::is_same< allocator, typename allocator_manager::allocator_t >::value >::type
init( std::allocator_arg_t, allocator const & alloc, in_place_t< source > t, arg && ... a )
{ init( in_place_t< wrapper< is_targetable, wrapper_allocator< allocator >, sig ... > >{}, std::allocator_arg, alloc, t, std::forward< arg >( a ) ... ); }
// Local erasures.
template< typename source, typename ... arg >
typename std::enable_if< is_small< source >::value MSVC_FIX( && ! is_noexcept_erasable< source >::value ) >::type
init( in_place_t< source >, arg && ... a )
{ new (storage_address()) local_erasure< source, typename implicit_object_to_parameter< sig >::type ... >( std::forward< arg >( a ) ... ); }
// Allocated erasures.
template< typename in_allocator, typename source, typename ... arg >
typename std::enable_if< ! is_compatibly_wrapped< source >::value && ! is_small< source >::value >::type
init( std::allocator_arg_t, in_allocator && alloc, in_place_t< source >, arg && ... a ) {
typedef typename std::allocator_traits< typename std::decay< in_allocator >::type >::template rebind_alloc< source > allocator;
typedef allocator_erasure< allocator, source, typename implicit_object_to_parameter< sig >::type ... > erasure;
static_assert ( is_allocator_erasure< erasure >::value, "" );
// 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( std::allocator_arg, alloc, std::forward< arg >( a ) ... );
}
template< typename source, typename ... arg >
typename std::enable_if< ! is_compatibly_wrapped< source >::value && ! is_small< source >::value >::type
init( in_place_t< source > t, arg && ... a )
{ init( std::allocator_arg, allocator_manager::actual_allocator(), t, std::forward< arg >( a ) ... ); }
wrapper & finish_assign ( wrapper && next ) noexcept {
destroy();
init( in_place_t< wrapper >{}, std::move( next ) );
this->actual_allocator() = next.actual_allocator();
return * this;
}
void destroy() noexcept {
auto nontrivial = std::get< + dispatch_slot::destructor >( this->erasure().table );
if ( nontrivial ) nontrivial( this->erasure(), allocator_manager::any_allocator() );
}
public:
using MSVC_SKIP(wrapper::)wrapper_base::operator ();
using MSVC_SKIP(wrapper::)wrapper_base::target;
using MSVC_SKIP(wrapper::)wrapper_base::target_type;
using MSVC_SKIP(wrapper::)wrapper_base::verify_type;
MSVC_SKIP ( using MSVC_SKIP(wrapper::)wrapper_base::operator bool; )
using MSVC_SKIP(wrapper::)wrapper_base::complete_object_address;
wrapper() noexcept
{ init( in_place_t< std::nullptr_t >{}, nullptr ); }
wrapper( wrapper && s ) noexcept
: allocator_manager( std::move( s ) )
{ init( in_place_t< wrapper >{}, std::move( s ) ); }
wrapper( wrapper const & s )
: allocator_manager( s )
{ init( in_place_t< wrapper >{}, s ); }
template< typename allocator >
wrapper( std::allocator_arg_t, allocator const & alloc )
: allocator_manager( std::allocator_arg, alloc )
{ init( in_place_t< std::nullptr_t >{}, nullptr ); }
template< typename source,
typename std::enable_if<
is_targetable< typename std::decay< source >::type >::value
&& std::is_constructible< typename std::decay< source >::type, source >::value
&& ! std::is_base_of< wrapper, 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 >::with_compatible_allocation )
{ init( in_place_t< typename std::decay< source >::type >{}, std::forward< source >( s ) ); }
// Prevent slicing fallback to copy/move constructor.
template< typename source,
typename std::enable_if<
! is_targetable< typename std::decay< source >::type >::value
|| ! std::is_constructible< typename std::decay< source >::type, source >::value
>::type * = nullptr >
wrapper( source && s ) = delete;
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 const & alloc, source && s )
noexcept( is_noexcept_erasable< typename std::decay< source >::type >::value )
: allocator_manager( std::allocator_arg, alloc )
{ init( std::allocator_arg, alloc, in_place_t< typename std::decay< source >::type >{}, std::forward< source >( s ) ); }
template< typename source, typename ... arg,
typename = typename std::enable_if< is_targetable< source >::value >::type >
wrapper( in_place_t< source > t, arg && ... a )
noexcept( is_noexcept_erasable< source >::value )
{ init( t, std::forward< arg >( a ) ... ); }
template< typename allocator, typename source, typename ... arg,
typename = typename std::enable_if<
is_targetable< source >::value
>::type >
wrapper( std::allocator_arg_t, allocator const & alloc, in_place_t< source > t, arg && ... a )
noexcept( is_noexcept_erasable< source >::value )
: allocator_manager( std::allocator_arg, alloc )
{ init( std::allocator_arg, alloc, t, std::forward< arg >( a ) ... ); }
~ wrapper() noexcept
{ destroy(); }
wrapper & operator = ( wrapper && s )
noexcept ( allocator_manager::noexcept_move_assign ) {
if ( & s == this ) return * this;
allocator_manager::operator = ( std::move( s ) );
return finish_assign( allocator_manager::template reallocate< wrapper >( std::move( s ) ) );
}
wrapper & operator = ( wrapper const & s ) {
if ( & s == this ) return * this;
allocator_manager::operator = ( s );
return finish_assign( allocator_manager::template reallocate< wrapper >( s ) );
}
template< typename source,
typename std::enable_if<
is_compatibly_wrapped< typename std::decay< source >::type >::value
&& std::is_constructible< typename std::decay< source >::type, source >::value
&& ! std::is_base_of< wrapper, typename std::decay< source >::type >::value
>::type * = nullptr >
wrapper &
operator = ( source && s )
noexcept( is_compatibly_wrapped< source >::with_compatible_allocation && allocator_manager::noexcept_move_assign )
{ return finish_assign( allocator_manager::template reallocate< wrapper >( std::forward< source >( s ) ) ); }
template< typename source,
typename std::enable_if<
is_targetable< typename std::decay< source >::type >::value
&& std::is_constructible< typename std::decay< source >::type, source >::value
&& ! is_compatibly_wrapped< typename std::decay< source >::type >::value
>::type * = nullptr >
wrapper &
operator = ( source && s )
noexcept( is_noexcept_erasable< typename std::decay< source >::type >::value )
{ return finish_assign( wrapper{ std::allocator_arg, this->actual_allocator(), std::forward< source >( s ) } ); }
template< typename allocator, typename source,
typename = typename std::enable_if< is_targetable< typename std::decay< source >::type >::value >::type >
wrapper &
assign( source && s, allocator const & alloc )
noexcept( is_noexcept_erasable< typename std::decay< source >::type >::value || is_compatibly_wrapped< source >::value )
{ return finish_assign( wrapper{ std::allocator_arg, alloc, std::forward< source >( s ) } ); }
template< typename source, typename ... arg,
typename = typename std::enable_if< is_targetable< source >::value >::type >
wrapper &
emplace_assign( arg && ... a )
noexcept( is_noexcept_erasable< source >::value )
{ return finish_assign( wrapper{ std::allocator_arg, this->actual_allocator(), in_place_t< source >{}, std::forward< arg >( a ) ... } ); }
template< typename source, typename allocator, typename ... arg,
typename = typename std::enable_if< is_targetable< source >::value >::type >
wrapper &
allocate_assign( allocator const & alloc, arg && ... a )
noexcept( is_noexcept_erasable< source >::value ) {
return finish_assign( wrapper< is_targetable, wrapper_allocator< allocator >, sig ... >
{ std::allocator_arg, alloc, in_place_t< source >{}, std::forward< arg >( a ) ... } );
}
void swap( wrapper & o ) noexcept {
this->allocator_manager::swap( o );
/* Use the two-assignment algorithm. Each target is safely restored to its original allocator.
Calling std::swap would be sufficient, except that POCMA might not be set. */
wrapper temp( std::move( * this ) );
destroy();
init( in_place_t< wrapper >{}, std::move( o ) );
o.destroy();
o.init( in_place_t< wrapper >{}, std::move( temp ) );
}
};
#undef UNPACK
#undef IGNORE
#undef DISPATCH_CQ
#undef DISPATCH_CV
#undef DISPATCH_CVREFQ
#undef DISPATCH_ALL
#undef DISPATCH_TABLE
}
// The actual classes all just pull their interfaces out of private inheritance.
template< typename ... sig >
class function
: impl::wrapper< impl::is_copyable_all_callable< function< sig ... >, sig ... >::template temp, impl::wrapper_no_allocator, sig ... > {
template< template< typename ... > class, typename, typename ... >
friend class impl::wrapper;
public:
MSVC_SKIP( using function::wrapper::wrapper; )
MSVC_FIX(
template< typename ... arg,
typename std::enable_if<
std::is_constructible< wrapper, arg ... >::value
>::type * = nullptr >
function( arg && ... a )
: wrapper( std::forward< arg >( a ) ... ) {}
)
MSVC_SKIP (
function() noexcept = default; // Investigate why these are needed. Compiler bug?
function( function && s ) noexcept = default;
function( function const & ) = default;
function & operator = ( function && s ) noexcept = default;