Refactor FOLLY_GCC_DISABLE_WARNING to play nice with clang-format
[folly.git] / folly / Traits.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // @author: Andrei Alexandrescu
18
19 #pragma once
20
21 #include <memory>
22 #include <limits>
23 #include <type_traits>
24 #include <functional>
25
26 #include <folly/Portability.h>
27
28 // libc++ doesn't provide this header, nor does msvc
29 #ifdef FOLLY_HAVE_BITS_CXXCONFIG_H
30 // This file appears in two locations: inside fbcode and in the
31 // libstdc++ source code (when embedding fbstring as std::string).
32 // To aid in this schizophrenic use, two macros are defined in
33 // c++config.h:
34 //   _LIBSTDCXX_FBSTRING - Set inside libstdc++.  This is useful to
35 //      gate use inside fbcode v. libstdc++
36 #include <bits/c++config.h>
37 #endif
38
39 #define FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(classname, type_name)              \
40   template <typename TTheClass_>                                               \
41   struct classname##__folly_traits_impl__ {                                    \
42     template <typename UTheClass_>                                             \
43     static constexpr bool test(typename UTheClass_::type_name*) {              \
44       return true;                                                             \
45     }                                                                          \
46     template <typename>                                                        \
47     static constexpr bool test(...) {                                          \
48       return false;                                                            \
49     }                                                                          \
50   };                                                                           \
51   template <typename TTheClass_>                                               \
52   using classname = typename std::conditional<                                 \
53       classname##__folly_traits_impl__<TTheClass_>::template test<TTheClass_>( \
54           nullptr),                                                            \
55       std::true_type,                                                          \
56       std::false_type>::type;
57
58 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, cv_qual) \
59   template <typename TTheClass_, typename RTheReturn_, typename... TTheArgs_> \
60   struct classname##__folly_traits_impl__<                                    \
61       TTheClass_,                                                             \
62       RTheReturn_(TTheArgs_...) cv_qual> {                                    \
63     template <                                                                \
64         typename UTheClass_,                                                  \
65         RTheReturn_ (UTheClass_::*)(TTheArgs_...) cv_qual>                    \
66     struct sfinae {};                                                         \
67     template <typename UTheClass_>                                            \
68     static std::true_type test(sfinae<UTheClass_, &UTheClass_::func_name>*);  \
69     template <typename>                                                       \
70     static std::false_type test(...);                                         \
71   }
72
73 /*
74  * The FOLLY_CREATE_HAS_MEMBER_FN_TRAITS is used to create traits
75  * classes that check for the existence of a member function with
76  * a given name and signature. It currently does not support
77  * checking for inherited members.
78  *
79  * Such classes receive two template parameters: the class to be checked
80  * and the signature of the member function. A static boolean field
81  * named `value` (which is also constexpr) tells whether such member
82  * function exists.
83  *
84  * Each traits class created is bound only to the member name, not to
85  * its signature nor to the type of the class containing it.
86  *
87  * Say you need to know if a given class has a member function named
88  * `test` with the following signature:
89  *
90  *    int test() const;
91  *
92  * You'd need this macro to create a traits class to check for a member
93  * named `test`, and then use this traits class to check for the signature:
94  *
95  * namespace {
96  *
97  * FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_test_traits, test);
98  *
99  * } // unnamed-namespace
100  *
101  * void some_func() {
102  *   cout << "Does class Foo have a member int test() const? "
103  *     << boolalpha << has_test_traits<Foo, int() const>::value;
104  * }
105  *
106  * You can use the same traits class to test for a completely different
107  * signature, on a completely different class, as long as the member name
108  * is the same:
109  *
110  * void some_func() {
111  *   cout << "Does class Foo have a member int test()? "
112  *     << boolalpha << has_test_traits<Foo, int()>::value;
113  *   cout << "Does class Foo have a member int test() const? "
114  *     << boolalpha << has_test_traits<Foo, int() const>::value;
115  *   cout << "Does class Bar have a member double test(const string&, long)? "
116  *     << boolalpha << has_test_traits<Bar, double(const string&, long)>::value;
117  * }
118  *
119  * @author: Marcelo Juchem <marcelo@fb.com>
120  */
121 #define FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(classname, func_name)               \
122   template <typename, typename>                                               \
123   struct classname##__folly_traits_impl__;                                    \
124   FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, );             \
125   FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(classname, func_name, const);        \
126   FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(                                     \
127       classname, func_name, /* nolint */ volatile);                           \
128   FOLLY_CREATE_HAS_MEMBER_FN_TRAITS_IMPL(                                     \
129       classname, func_name, /* nolint */ volatile const);                     \
130   template <typename TTheClass_, typename TTheSignature_>                     \
131   using classname =                                                           \
132       decltype(classname##__folly_traits_impl__<TTheClass_, TTheSignature_>:: \
133                    template test<TTheClass_>(nullptr))
134
135 namespace folly {
136
137 /***
138  *  _t
139  *
140  *  Instead of:
141  *
142  *    using decayed = typename std::decay<T>::type;
143  *
144  *  With the C++14 standard trait aliases, we could use:
145  *
146  *    using decayed = std::decay_t<T>;
147  *
148  *  Without them, we could use:
149  *
150  *    using decayed = _t<std::decay<T>>;
151  *
152  *  Also useful for any other library with template types having dependent
153  *  member types named `type`, like the standard trait types.
154  */
155 template <typename T>
156 using _t = typename T::type;
157
158 /**
159  *  void_t
160  *
161  *  A type alias for `void`. `void_t` is useful for controling class-template
162  *  partial specialization.
163  *
164  *  Example:
165  *
166  *    // has_value_type<T>::value is true if T has a nested type `value_type`
167  *    template <class T, class = void>
168  *    struct has_value_type
169  *        : std::false_type {};
170  *
171  *    template <class T>
172  *    struct has_value_type<T, folly::void_t<typename T::value_type>>
173  *        : std::true_type {};
174  */
175 #if defined(__cpp_lib_void_t) || defined(_MSC_VER)
176
177 /* using override */ using std::void_t;
178
179 #else // defined(__cpp_lib_void_t) || defined(_MSC_VER)
180
181 namespace traits_detail {
182 template <class...>
183 struct void_t_ {
184   using type = void;
185 };
186 } // namespace traits_detail
187
188 template <class... Ts>
189 using void_t = _t<traits_detail::void_t_<Ts...>>;
190
191 #endif // defined(__cpp_lib_void_t) || defined(_MSC_VER)
192
193 /**
194  * IsRelocatable<T>::value describes the ability of moving around
195  * memory a value of type T by using memcpy (as opposed to the
196  * conservative approach of calling the copy constructor and then
197  * destroying the old temporary. Essentially for a relocatable type,
198  * the following two sequences of code should be semantically
199  * equivalent:
200  *
201  * void move1(T * from, T * to) {
202  *   new(to) T(from);
203  *   (*from).~T();
204  * }
205  *
206  * void move2(T * from, T * to) {
207  *   memcpy(to, from, sizeof(T));
208  * }
209  *
210  * Most C++ types are relocatable; the ones that aren't would include
211  * internal pointers or (very rarely) would need to update remote
212  * pointers to pointers tracking them. All C++ primitive types and
213  * type constructors are relocatable.
214  *
215  * This property can be used in a variety of optimizations. Currently
216  * fbvector uses this property intensively.
217  *
218  * The default conservatively assumes the type is not
219  * relocatable. Several specializations are defined for known
220  * types. You may want to add your own specializations. Do so in
221  * namespace folly and make sure you keep the specialization of
222  * IsRelocatable<SomeStruct> in the same header as SomeStruct.
223  *
224  * You may also declare a type to be relocatable by including
225  *    `typedef std::true_type IsRelocatable;`
226  * in the class header.
227  *
228  * It may be unset in a base class by overriding the typedef to false_type.
229  */
230 /*
231  * IsTriviallyCopyable describes the value semantics property. C++11 contains
232  * the type trait is_trivially_copyable; however, it is not yet implemented
233  * in gcc (as of 4.7.1), and the user may wish to specify otherwise.
234  */
235 /*
236  * IsZeroInitializable describes the property that default construction is the
237  * same as memset(dst, 0, sizeof(T)).
238  */
239
240 namespace traits_detail {
241
242 #define FOLLY_HAS_TRUE_XXX(name)                                             \
243   FOLLY_CREATE_HAS_MEMBER_TYPE_TRAITS(has_##name, name);                     \
244   template <class T>                                                         \
245   struct name##_is_true : std::is_same<typename T::name, std::true_type> {}; \
246   template <class T>                                                         \
247   struct has_true_##name : std::conditional<                                 \
248                                has_##name<T>::value,                         \
249                                name##_is_true<T>,                            \
250                                std::false_type>::type {};
251
252 FOLLY_HAS_TRUE_XXX(IsRelocatable)
253 FOLLY_HAS_TRUE_XXX(IsZeroInitializable)
254 FOLLY_HAS_TRUE_XXX(IsTriviallyCopyable)
255
256 #undef FOLLY_HAS_TRUE_XXX
257
258 // Older versions of libstdc++ do not provide std::is_trivially_copyable
259 #if defined(__clang__) && !defined(_LIBCPP_VERSION)
260 template <class T>
261 struct is_trivially_copyable
262     : std::integral_constant<bool, __is_trivially_copyable(T)> {};
263 #elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
264 template <class T>
265 struct is_trivially_copyable : std::is_trivial<T> {};
266 #else
267 template <class T>
268 using is_trivially_copyable = std::is_trivially_copyable<T>;
269 #endif
270 }
271
272 struct Ignore {
273   template <class T>
274   /* implicit */ Ignore(const T&) {}
275   template <class T>
276   const Ignore& operator=(T const&) const { return *this; }
277 };
278
279 template <class...>
280 using Ignored = Ignore;
281
282 namespace traits_detail_IsEqualityComparable {
283 Ignore operator==(Ignore, Ignore);
284
285 template <class T, class U = T>
286 struct IsEqualityComparable
287     : std::is_convertible<
288           decltype(std::declval<T>() == std::declval<U>()),
289           bool
290       > {};
291 }
292
293 /* using override */ using traits_detail_IsEqualityComparable::
294     IsEqualityComparable;
295
296 namespace traits_detail_IsLessThanComparable {
297 Ignore operator<(Ignore, Ignore);
298
299 template <class T, class U = T>
300 struct IsLessThanComparable
301     : std::is_convertible<
302           decltype(std::declval<T>() < std::declval<U>()),
303           bool
304       > {};
305 }
306
307 /* using override */ using traits_detail_IsLessThanComparable::
308     IsLessThanComparable;
309
310 namespace traits_detail_IsNothrowSwappable {
311 #if defined(__cpp_lib_is_swappable) || (_CPPLIB_VER && _HAS_CXX17)
312 // MSVC 2015+ already implements the C++17 P0185R1 proposal which
313 // adds std::is_nothrow_swappable, so use it instead if C++17 mode
314 // is enabled.
315 template <typename T>
316 using IsNothrowSwappable = std::is_nothrow_swappable<T>;
317 #elif _CPPLIB_VER
318 // MSVC 2015+ defines the base even if C++17 is disabled, and
319 // MSVC 2015 has issues with our fallback implementation due to
320 // over-eager evaluation of noexcept.
321 template <typename T>
322 using IsNothrowSwappable = std::_Is_nothrow_swappable<T>;
323 #else
324 /* using override */ using std::swap;
325
326 template <class T>
327 struct IsNothrowSwappable
328     : std::integral_constant<bool,
329         std::is_nothrow_move_constructible<T>::value &&
330         noexcept(swap(std::declval<T&>(), std::declval<T&>()))
331       > {};
332 #endif
333 }
334
335 /* using override */ using traits_detail_IsNothrowSwappable::IsNothrowSwappable;
336
337 template <class T> struct IsTriviallyCopyable
338   : std::conditional<
339       traits_detail::has_IsTriviallyCopyable<T>::value,
340       traits_detail::has_true_IsTriviallyCopyable<T>,
341       traits_detail::is_trivially_copyable<T>
342     >::type {};
343
344 template <class T> struct IsRelocatable
345   : std::conditional<
346       traits_detail::has_IsRelocatable<T>::value,
347       traits_detail::has_true_IsRelocatable<T>,
348       // TODO add this line (and some tests for it) when we upgrade to gcc 4.7
349       //std::is_trivially_move_constructible<T>::value ||
350       IsTriviallyCopyable<T>
351     >::type {};
352
353 template <class T> struct IsZeroInitializable
354   : std::conditional<
355       traits_detail::has_IsZeroInitializable<T>::value,
356       traits_detail::has_true_IsZeroInitializable<T>,
357       std::integral_constant<bool, !std::is_class<T>::value>
358     >::type {};
359
360 template <typename...>
361 struct Conjunction : std::true_type {};
362 template <typename T>
363 struct Conjunction<T> : T {};
364 template <typename T, typename... TList>
365 struct Conjunction<T, TList...>
366     : std::conditional<T::value, Conjunction<TList...>, T>::type {};
367
368 template <typename...>
369 struct Disjunction : std::false_type {};
370 template <typename T>
371 struct Disjunction<T> : T {};
372 template <typename T, typename... TList>
373 struct Disjunction<T, TList...>
374     : std::conditional<T::value, T, Disjunction<TList...>>::type {};
375
376 template <typename T>
377 struct Negation : std::integral_constant<bool, !T::value> {};
378
379 template <bool... Bs>
380 struct Bools {
381   using valid_type = bool;
382   static constexpr std::size_t size() {
383     return sizeof...(Bs);
384   }
385 };
386
387 // Lighter-weight than Conjunction, but evaluates all sub-conditions eagerly.
388 template <class... Ts>
389 using StrictConjunction =
390     std::is_same<Bools<Ts::value..., true>, Bools<true, Ts::value...>>;
391
392 } // namespace folly
393
394 /**
395  * Use this macro ONLY inside namespace folly. When using it with a
396  * regular type, use it like this:
397  *
398  * // Make sure you're at namespace ::folly scope
399  * template<> FOLLY_ASSUME_RELOCATABLE(MyType)
400  *
401  * When using it with a template type, use it like this:
402  *
403  * // Make sure you're at namespace ::folly scope
404  * template<class T1, class T2>
405  * FOLLY_ASSUME_RELOCATABLE(MyType<T1, T2>)
406  */
407 #define FOLLY_ASSUME_RELOCATABLE(...) \
408   struct IsRelocatable<  __VA_ARGS__ > : std::true_type {};
409
410 /**
411  * The FOLLY_ASSUME_FBVECTOR_COMPATIBLE* macros below encode the
412  * assumption that the type is relocatable per IsRelocatable
413  * above. Many types can be assumed to satisfy this condition, but
414  * it is the responsibility of the user to state that assumption.
415  * User-defined classes will not be optimized for use with
416  * fbvector (see FBVector.h) unless they state that assumption.
417  *
418  * Use FOLLY_ASSUME_FBVECTOR_COMPATIBLE with regular types like this:
419  *
420  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE(MyType)
421  *
422  * The versions FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1, _2, _3, and _4
423  * allow using the macro for describing templatized classes with 1, 2,
424  * 3, and 4 template parameters respectively. For template classes
425  * just use the macro with the appropriate number and pass the name of
426  * the template to it. Example:
427  *
428  * template <class T1, class T2> class MyType { ... };
429  * ...
430  * // Make sure you're at global scope
431  * FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(MyType)
432  */
433
434 // Use this macro ONLY at global level (no namespace)
435 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE(...) \
436   namespace folly {                           \
437   template <>                                 \
438   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__)       \
439   }
440 // Use this macro ONLY at global level (no namespace)
441 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(...) \
442   namespace folly {                             \
443   template <class T1>                           \
444   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1>)     \
445   }
446 // Use this macro ONLY at global level (no namespace)
447 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(...) \
448   namespace folly {                             \
449   template <class T1, class T2>                 \
450   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2>) \
451   }
452 // Use this macro ONLY at global level (no namespace)
453 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(...)     \
454   namespace folly {                                 \
455   template <class T1, class T2, class T3>           \
456   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3>) \
457   }
458 // Use this macro ONLY at global level (no namespace)
459 #define FOLLY_ASSUME_FBVECTOR_COMPATIBLE_4(...)         \
460   namespace folly {                                     \
461   template <class T1, class T2, class T3, class T4>     \
462   FOLLY_ASSUME_RELOCATABLE(__VA_ARGS__<T1, T2, T3, T4>) \
463   }
464
465 /**
466  * Instantiate FOLLY_ASSUME_FBVECTOR_COMPATIBLE for a few types. It is
467  * safe to assume that pair is compatible if both of its components
468  * are. Furthermore, all STL containers can be assumed to comply,
469  * although that is not guaranteed by the standard.
470  */
471
472 FOLLY_NAMESPACE_STD_BEGIN
473
474 template <class T, class U>
475   struct pair;
476 #ifndef _GLIBCXX_USE_FB
477 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
478 template <class T, class R, class A>
479   class basic_string;
480 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
481 #else
482 template <class T, class R, class A, class S>
483   class basic_string;
484 #endif
485 template <class T, class A>
486   class vector;
487 template <class T, class A>
488   class deque;
489 FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
490 template <class T, class A>
491   class list;
492 FOLLY_GLIBCXX_NAMESPACE_CXX11_END
493 template <class T, class C, class A>
494   class set;
495 template <class K, class V, class C, class A>
496   class map;
497 template <class T>
498   class shared_ptr;
499
500 FOLLY_NAMESPACE_STD_END
501
502 namespace folly {
503
504 // STL commonly-used types
505 template <class T, class U>
506 struct IsRelocatable< std::pair<T, U> >
507     : std::integral_constant<bool,
508         IsRelocatable<T>::value &&
509         IsRelocatable<U>::value> {};
510
511 // Is T one of T1, T2, ..., Tn?
512 template <class T, class... Ts>
513 struct IsOneOf {
514   enum { value = false };
515 };
516
517 template <class T, class T1, class... Ts>
518 struct IsOneOf<T, T1, Ts...> {
519   enum { value = std::is_same<T, T1>::value || IsOneOf<T, Ts...>::value };
520 };
521
522 /*
523  * Complementary type traits for integral comparisons.
524  *
525  * For instance, `if(x < 0)` yields an error in clang for unsigned types
526  *  when -Werror is used due to -Wtautological-compare
527  *
528  *
529  * @author: Marcelo Juchem <marcelo@fb.com>
530  */
531
532 namespace detail {
533
534 template <typename T, bool>
535 struct is_negative_impl {
536   constexpr static bool check(T x) { return x < 0; }
537 };
538
539 template <typename T>
540 struct is_negative_impl<T, false> {
541   constexpr static bool check(T) { return false; }
542 };
543
544 // folly::to integral specializations can end up generating code
545 // inside what are really static ifs (not executed because of the templated
546 // types) that violate -Wsign-compare and/or -Wbool-compare so suppress them
547 // in order to not prevent all calling code from using it.
548 FOLLY_PUSH_WARNING
549 FOLLY_GCC_DISABLE_WARNING("-Wsign-compare")
550 #if __GNUC_PREREQ(5, 0)
551 FOLLY_GCC_DISABLE_WARNING("-Wbool-compare")
552 #endif
553 FOLLY_MSVC_DISABLE_WARNING(4388) // sign-compare
554 FOLLY_MSVC_DISABLE_WARNING(4804) // bool-compare
555
556 template <typename RHS, RHS rhs, typename LHS>
557 bool less_than_impl(LHS const lhs) {
558   return
559     rhs > std::numeric_limits<LHS>::max() ? true :
560     rhs <= std::numeric_limits<LHS>::min() ? false :
561     lhs < rhs;
562 }
563
564 template <typename RHS, RHS rhs, typename LHS>
565 bool greater_than_impl(LHS const lhs) {
566   return
567     rhs > std::numeric_limits<LHS>::max() ? false :
568     rhs < std::numeric_limits<LHS>::min() ? true :
569     lhs > rhs;
570 }
571
572 FOLLY_POP_WARNING
573
574 } // namespace detail {
575
576 // same as `x < 0`
577 template <typename T>
578 constexpr bool is_negative(T x) {
579   return folly::detail::is_negative_impl<T, std::is_signed<T>::value>::check(x);
580 }
581
582 // same as `x <= 0`
583 template <typename T>
584 constexpr bool is_non_positive(T x) { return !x || folly::is_negative(x); }
585
586 // same as `x > 0`
587 template <typename T>
588 constexpr bool is_positive(T x) { return !is_non_positive(x); }
589
590 // same as `x >= 0`
591 template <typename T>
592 constexpr bool is_non_negative(T x) {
593   return !x || is_positive(x);
594 }
595
596 template <typename RHS, RHS rhs, typename LHS>
597 bool less_than(LHS const lhs) {
598   return detail::less_than_impl<
599     RHS, rhs, typename std::remove_reference<LHS>::type
600   >(lhs);
601 }
602
603 template <typename RHS, RHS rhs, typename LHS>
604 bool greater_than(LHS const lhs) {
605   return detail::greater_than_impl<
606     RHS, rhs, typename std::remove_reference<LHS>::type
607   >(lhs);
608 }
609
610 namespace traits_detail {
611 struct InPlaceTag {};
612 template <class>
613 struct InPlaceTypeTag {};
614 template <std::size_t>
615 struct InPlaceIndexTag {};
616 }
617
618 /**
619  * Like std::piecewise_construct, a tag type & instance used for in-place
620  * construction of non-movable contained types, e.g. by Synchronized.
621  * Follows the naming and design of std::in_place suggested in
622  * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0032r2.pdf
623  */
624 using in_place_t = traits_detail::InPlaceTag (&)(traits_detail::InPlaceTag);
625
626 template <class T>
627 using in_place_type_t =
628     traits_detail::InPlaceTypeTag<T> (&)(traits_detail::InPlaceTypeTag<T>);
629
630 template <std::size_t I>
631 using in_place_index_t =
632     traits_detail::InPlaceIndexTag<I> (&)(traits_detail::InPlaceIndexTag<I>);
633
634 inline traits_detail::InPlaceTag in_place(traits_detail::InPlaceTag = {}) {
635   return {};
636 }
637
638 template <class T>
639 inline traits_detail::InPlaceTypeTag<T> in_place(
640     traits_detail::InPlaceTypeTag<T> = {}) {
641   return {};
642 }
643
644 template <std::size_t I>
645 inline traits_detail::InPlaceIndexTag<I> in_place(
646     traits_detail::InPlaceIndexTag<I> = {}) {
647   return {};
648 }
649
650 // For backwards compatibility:
651 using construct_in_place_t = in_place_t;
652
653 inline traits_detail::InPlaceTag construct_in_place(
654     traits_detail::InPlaceTag = {}) {
655   return {};
656 }
657
658 /**
659  * Initializer lists are a powerful compile time syntax introduced in C++11
660  * but due to their often conflicting syntax they are not used by APIs for
661  * construction.
662  *
663  * Further standard conforming compilers *strongly* favor an
664  * std::initalizer_list overload for construction if one exists.  The
665  * following is a simple tag used to disambiguate construction with
666  * initializer lists and regular uniform initialization.
667  *
668  * For example consider the following case
669  *
670  *  class Something {
671  *  public:
672  *    explicit Something(int);
673  *    Something(std::intiializer_list<int>);
674  *
675  *    operator int();
676  *  };
677  *
678  *  ...
679  *  Something something{1}; // SURPRISE!!
680  *
681  * The last call to instantiate the Something object will go to the
682  * initializer_list overload.  Which may be surprising to users.
683  *
684  * If however this tag was used to disambiguate such construction it would be
685  * easy for users to see which construction overload their code was referring
686  * to.  For example
687  *
688  *  class Something {
689  *  public:
690  *    explicit Something(int);
691  *    Something(folly::initlist_construct_t, std::initializer_list<int>);
692  *
693  *    operator int();
694  *  };
695  *
696  *  ...
697  *  Something something_one{1}; // not the initializer_list overload
698  *  Something something_two{folly::initlist_construct, {1}}; // correct
699  */
700 struct initlist_construct_t {};
701 constexpr initlist_construct_t initlist_construct{};
702
703 } // namespace folly
704
705 // Assume nothing when compiling with MSVC.
706 #ifndef _MSC_VER
707 // gcc-5.0 changed string's implementation in libstdc++ to be non-relocatable
708 #if !_GLIBCXX_USE_CXX11_ABI
709 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_3(std::basic_string)
710 #endif
711 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::vector)
712 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::list)
713 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::deque)
714 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_2(std::unique_ptr)
715 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::shared_ptr)
716 FOLLY_ASSUME_FBVECTOR_COMPATIBLE_1(std::function)
717 #endif
718
719 /* Some combinations of compilers and C++ libraries make __int128 and
720  * unsigned __int128 available but do not correctly define their standard type
721  * traits.
722  *
723  * If FOLLY_SUPPLY_MISSING_INT128_TRAITS is defined, we define these traits
724  * here.
725  *
726  * @author: Phil Willoughby <philwill@fb.com>
727  */
728 #if FOLLY_SUPPLY_MISSING_INT128_TRAITS
729 FOLLY_NAMESPACE_STD_BEGIN
730 template <>
731 struct is_arithmetic<__int128> : ::std::true_type {};
732 template <>
733 struct is_arithmetic<unsigned __int128> : ::std::true_type {};
734 template <>
735 struct is_integral<__int128> : ::std::true_type {};
736 template <>
737 struct is_integral<unsigned __int128> : ::std::true_type {};
738 template <>
739 struct make_unsigned<__int128> {
740   typedef unsigned __int128 type;
741 };
742 template <>
743 struct make_signed<__int128> {
744   typedef __int128 type;
745 };
746 template <>
747 struct make_unsigned<unsigned __int128> {
748   typedef unsigned __int128 type;
749 };
750 template <>
751 struct make_signed<unsigned __int128> {
752   typedef __int128 type;
753 };
754 template <>
755 struct is_signed<__int128> : ::std::true_type {};
756 template <>
757 struct is_unsigned<unsigned __int128> : ::std::true_type {};
758 FOLLY_NAMESPACE_STD_END
759 #endif // FOLLY_SUPPLY_MISSING_INT128_TRAITS