1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef PDFIUM_THIRD_PARTY_BASE_TEMPLATE_UTIL_H_
6 #define PDFIUM_THIRD_PARTY_BASE_TEMPLATE_UTIL_H_
8 #include <cstddef> // For size_t.
13 template<class T, T v>
14 struct integral_constant {
15 static const T value = v;
17 typedef integral_constant<T, v> type;
20 typedef integral_constant<bool, true> true_type;
21 typedef integral_constant<bool, false> false_type;
23 template <class T, class U> struct is_same : public false_type {};
25 struct is_same<T, T> : true_type {};
27 template<bool B, class T = void>
31 struct enable_if<true, T> { typedef T type; };
35 // Types YesType and NoType are guaranteed such that sizeof(YesType) <
43 // This class is an implementation detail for is_convertible, and you
44 // don't need to know how it works to use is_convertible. For those
45 // who care: we declare two different functions, one whose argument is
46 // of type To and one with a variadic argument list. We give them
47 // return types of different size, so we can use sizeof to trick the
48 // compiler into telling us which function it would have chosen if we
49 // had called it with an argument of type From. See Alexandrescu's
50 // _Modern C++ Design_ for more details on this sort of trick.
52 struct ConvertHelper {
53 template <typename To>
54 static YesType Test(To);
56 template <typename To>
57 static NoType Test(...);
59 template <typename From>
60 static From& Create();
63 } // namespace internal
65 // Inherits from true_type if From is convertible to To, false_type otherwise.
67 // Note that if the type is convertible, this will be a true_type REGARDLESS
68 // of whether or not the conversion would emit a warning.
69 template <typename From, typename To>
71 : integral_constant<bool,
72 sizeof(internal::ConvertHelper::Test<To>(
73 internal::ConvertHelper::Create<From>())) ==
74 sizeof(internal::YesType)> {};
79 #endif // PDFIUM_THIRD_PARTY_BASE_TEMPLATE_UTIL_H_