From ccb56f3caf60f77d912704fb3a221bc9f12dbfb7 Mon Sep 17 00:00:00 2001 From: Phil Willoughby Date: Tue, 1 Aug 2017 12:20:30 -0700 Subject: [PATCH] C++11 support for Replaceable Summary: Also add tests for the `is_replaceable` trait. Reviewed By: yfeldblum Differential Revision: D5526317 fbshipit-source-id: 92559d55fbb8d115856ef9e8f86b42e327f74e56 --- folly/FixedString.h | 19 +------------------ folly/Portability.h | 18 ++++++++++++++++++ folly/Replaceable.h | 15 ++++++++------- folly/test/ReplaceableTest.cpp | 4 ++++ 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/folly/FixedString.h b/folly/FixedString.h index bf0d52e3..c6925bbf 100644 --- a/folly/FixedString.h +++ b/folly/FixedString.h @@ -28,29 +28,12 @@ #include #include +#include #include #include #include #include -// Define FOLLY_USE_CPP14_CONSTEXPR to be true if the compiler's C++14 -// constexpr support is "good enough". -#ifndef FOLLY_USE_CPP14_CONSTEXPR -#if defined(__clang__) -#define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201300L -#elif defined(__GNUC__) -#define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201304L -#else -#define FOLLY_USE_CPP14_CONSTEXPR 0 // MSVC? -#endif -#endif - -#if FOLLY_USE_CPP14_CONSTEXPR -#define FOLLY_CPP14_CONSTEXPR constexpr -#else -#define FOLLY_CPP14_CONSTEXPR inline -#endif - namespace folly { template diff --git a/folly/Portability.h b/folly/Portability.h index 0f549868..cf2c8d32 100644 --- a/folly/Portability.h +++ b/folly/Portability.h @@ -407,3 +407,21 @@ constexpr auto kIsWindows = false; constexpr auto kMscVer = 0; #endif } + +// Define FOLLY_USE_CPP14_CONSTEXPR to be true if the compiler's C++14 +// constexpr support is "good enough". +#ifndef FOLLY_USE_CPP14_CONSTEXPR +#if defined(__clang__) +#define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201300L +#elif defined(__GNUC__) +#define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201304L +#else +#define FOLLY_USE_CPP14_CONSTEXPR 0 // MSVC? +#endif +#endif + +#if FOLLY_USE_CPP14_CONSTEXPR +#define FOLLY_CPP14_CONSTEXPR constexpr +#else +#define FOLLY_CPP14_CONSTEXPR inline +#endif diff --git a/folly/Replaceable.h b/folly/Replaceable.h index 011b1f8b..b786f8e8 100644 --- a/folly/Replaceable.h +++ b/folly/Replaceable.h @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -450,7 +451,7 @@ class alignas(T) Replaceable template < class... Args, std::enable_if_t::value, int> = 0> - constexpr explicit Replaceable(in_place_t, Args&&... args) + FOLLY_CPP14_CONSTEXPR explicit Replaceable(in_place_t, Args&&... args) // clang-format off noexcept(std::is_nothrow_constructible::value) // clang-format on @@ -464,7 +465,7 @@ class alignas(T) Replaceable std::enable_if_t< std::is_constructible, Args&&...>::value, int> = 0> - constexpr explicit Replaceable( + FOLLY_CPP14_CONSTEXPR explicit Replaceable( in_place_t, std::initializer_list il, Args&&... args) @@ -486,7 +487,7 @@ class alignas(T) Replaceable !std::is_same, std::decay_t>::value && std::is_convertible::value, int> = 0> - constexpr /* implicit */ Replaceable(U&& other) + FOLLY_CPP14_CONSTEXPR /* implicit */ Replaceable(U&& other) // clang-format off noexcept(std::is_nothrow_constructible::value) // clang-format on @@ -502,7 +503,7 @@ class alignas(T) Replaceable !std::is_same, std::decay_t>::value && !std::is_convertible::value, int> = 0> - explicit constexpr Replaceable(U&& other) + FOLLY_CPP14_CONSTEXPR explicit Replaceable(U&& other) // clang-format off noexcept(std::is_nothrow_constructible::value) // clang-format on @@ -622,7 +623,7 @@ class alignas(T) Replaceable return launder(reinterpret_cast(storage_)); } - constexpr T* operator->() { + FOLLY_CPP14_CONSTEXPR T* operator->() { return launder(reinterpret_cast(storage_)); } @@ -630,11 +631,11 @@ class alignas(T) Replaceable return *launder(reinterpret_cast(storage_)); } - constexpr T& operator*() & { + FOLLY_CPP14_CONSTEXPR T& operator*() & { return *launder(reinterpret_cast(storage_)); } - constexpr T&& operator*() && { + FOLLY_CPP14_CONSTEXPR T&& operator*() && { return std::move(*launder(reinterpret_cast(storage_))); } diff --git a/folly/test/ReplaceableTest.cpp b/folly/test/ReplaceableTest.cpp index 9e29ddaa..5bc6b622 100644 --- a/folly/test/ReplaceableTest.cpp +++ b/folly/test/ReplaceableTest.cpp @@ -170,6 +170,10 @@ TYPED_TEST(ReplaceableStaticAttributeTest, nothrow_copy_assignable) { std::is_nothrow_copy_constructible::value, std::is_nothrow_copy_assignable>::value); } +TYPED_TEST(ReplaceableStaticAttributeTest, replaceable) { + EXPECT_FALSE(is_replaceable::value); + EXPECT_TRUE(is_replaceable>::value); +} TYPED_TEST(ReplaceableStaticAttributePairTest, copy_construct) { using T = typename TypeParam::first_type; -- 2.34.1