Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2883)

Unified Diff: base/type_traits.h

Issue 2944523002: Improving flat containers interface. (Closed)
Patch Set: Review, round 1. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/containers/flat_tree_unittest.cc ('K') | « base/functional_unittest.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/type_traits.h
diff --git a/base/type_traits.h b/base/type_traits.h
new file mode 100644
index 0000000000000000000000000000000000000000..4e12884ef26d81671abefb1964ceb5423fedc569
--- /dev/null
+++ b/base/type_traits.h
@@ -0,0 +1,76 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
jdoerrie 2017/06/20 23:51:09 base/template_util.h (https://codesearch.chromium.
dyaroshev 2017/06/22 10:41:43 Done - merged it here. Also replaced local impleme
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_TYPE_TRAITS_H_
+#define BASE_TYPE_TRAITS_H_
+
+#include <type_traits>
+
+namespace base {
+
+// C++14 ---------------------------------------------------------------------
+// These can be replaced with the ones from the standard once we have C++14.
+
+// std::enable_if_t: http://en.cppreference.com/w/cpp/types/enable_if
+template <bool condition, typename T = void>
+using enable_if_t = typename std::enable_if<condition, T>::type;
+
+// std::conditional_t: http://en.cppreference.com/w/cpp/types/conditional
+template <bool condition, typename T, typename U>
+using conditional_t = typename std::conditional<condition, T, U>::type;
+
+// C++17 ----------------------------------------------------------------------
+// These can be replaced with the ones from the standard once we have C++17.
+
+// std::void_t: http://en.cppreference.com/w/cpp/types/void_t
+// Needed for the detection idiom.
+template <typename...>
+using void_t = void;
+
+// std::nonesuch: http://en.cppreference.com/w/cpp/experimental/nonesuch
+// Needed for the detection idiom.
+struct nonesuch {
+ nonesuch() = delete;
+ ~nonesuch() = delete;
+ nonesuch(nonesuch const&) = delete;
+ void operator=(nonesuch const&) = delete;
+};
+
+// Implementation of the detection idiom from cppreference:
+// http://en.cppreference.com/w/cpp/experimental/is_detected
+namespace internal {
+
+template <typename Default,
+ typename AlwaysVoid,
+ template <typename...> class Op,
+ typename... Args>
+struct detector {
+ using value_t = std::false_type;
+ using type = Default;
+};
+
+template <typename Default, template <typename...> class Op, typename... Args>
+struct detector<Default, void_t<Op<Args...>>, Op, Args...> {
+ using value_t = std::true_type;
+ using type = Op<Args...>;
+};
+
+} // namespace internal
+
+// std::is_detected: http://en.cppreference.com/w/cpp/experimental/is_detected
+template <template <class...> class Op, typename... Args>
+using is_detected =
+ typename internal::detector<nonesuch, void, Op, Args...>::value_t;
+
+// std::is_detected_v, implemented as a constexpr function instead of a variable
+// template, since we don't have those.
+// http://en.cppreference.com/w/cpp/experimental/is_detected
+template <template <typename...> class Op, typename... Args>
+constexpr bool is_detected_c() {
+ return is_detected<Op, Args...>::value;
+}
+
+} // namespace base
+
+#endif // BASE_TYPE_TRAITS_H_
« base/containers/flat_tree_unittest.cc ('K') | « base/functional_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698