[compiler-rt] r359334 - Implement __sanitizer::conditional<B, T, F>
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 26 11:22:55 PDT 2019
Author: vitalybuka
Date: Fri Apr 26 11:22:55 2019
New Revision: 359334
URL: http://llvm.org/viewvc/llvm-project?rev=359334&view=rev
Log:
Implement __sanitizer::conditional<B, T, F>
Modified:
compiler-rt/trunk/lib/sanitizer_common/sanitizer_type_traits.h
compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_type_traits_test.cc
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_type_traits.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_type_traits.h?rev=359334&r1=359333&r2=359334&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_type_traits.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_type_traits.h Fri Apr 26 11:22:55 2019
@@ -38,6 +38,25 @@ struct is_same : public false_type {};
template <typename T>
struct is_same<T, T> : public true_type {};
+// conditional<B, T, F>
+//
+// Defines type as T if B is true or as F otherwise.
+// E.g. the following is true
+//
+// ```
+// is_same<int, conditional<true, int, double>::type>::value
+// is_same<double, conditional<false, int, double>::type>::value
+// ```
+template <bool B, class T, class F>
+struct conditional {
+ using type = T;
+};
+
+template <class T, class F>
+struct conditional<false, T, F> {
+ using type = F;
+};
+
} // namespace __sanitizer
#endif
Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_type_traits_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_type_traits_test.cc?rev=359334&r1=359333&r2=359334&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_type_traits_test.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_type_traits_test.cc Fri Apr 26 11:22:55 2019
@@ -25,3 +25,8 @@ TEST(SanitizerCommon, IsSame) {
ASSERT_FALSE((is_same<uptr, sptr>::value));
ASSERT_FALSE((is_same<uptr, const uptr>::value));
}
+
+TEST(SanitizerCommon, Conditional) {
+ ASSERT_TRUE((is_same<int, conditional<true, int, double>::type>::value));
+ ASSERT_TRUE((is_same<double, conditional<false, int, double>::type>::value));
+}
More information about the llvm-commits
mailing list