Commit cd3b981a authored by Joe Loser's avatar Joe Loser Committed by Facebook Github Bot

Cut FOLLY_USE_CPP14_CONSTEXPR and FOLLY_CPP14_CONSTEXPR (#1029)

Summary:
- `FOLLY_USE_CPP14_CONSTEXPR` macro is not needed as Folly requires a
  recent enough version of MSVC where its constexpr support is "good
  enough". For Clang and GCC, the min compiler versions supported would
  both evaluate the prior implementation of this macro to true in both
  cases. This is potentially slightly behavior changing since
  `FOLLY_USE_CPP14_CONSTEXPR` would be `inline` for ICC and I am not
  sure if its constexpr support is "good enough" for a min version of
  ICC we claim support for.
- Replace `FOLLY_CPP14_CONSTEXPR` with `constexpr` in all call sites and
  remove the `FOLLY_CPP14_CONSTEXPR` macro.
- Simplify how we define `FOLLY_STORAGE_CONSTEXPR` and
  `FOLLY_STORAGE_CPP14_CONSTEXPR` after cutting
  `FOLLY_USE_CPP14_CONSTEXPR`.
Pull Request resolved: https://github.com/facebook/folly/pull/1029

Reviewed By: Orvid

Differential Revision: D14199538

Pulled By: yfeldblum

fbshipit-source-id: 99daecf7d7ad0c4bf6735e74247112a78923602a
parent 02e56189
This diff is collapsed.
......@@ -104,7 +104,7 @@ class Optional {
!std::is_abstract<Value>::value,
"Optional may not be used with abstract types");
FOLLY_CPP14_CONSTEXPR Optional() noexcept {}
constexpr Optional() noexcept {}
Optional(const Optional& src) noexcept(
std::is_nothrow_copy_constructible<Value>::value) {
......@@ -121,14 +121,14 @@ class Optional {
}
}
FOLLY_CPP14_CONSTEXPR /* implicit */ Optional(const None&) noexcept {}
constexpr /* implicit */ Optional(const None&) noexcept {}
FOLLY_CPP14_CONSTEXPR /* implicit */ Optional(Value&& newValue) noexcept(
constexpr /* implicit */ Optional(Value&& newValue) noexcept(
std::is_nothrow_move_constructible<Value>::value) {
construct(std::move(newValue));
}
FOLLY_CPP14_CONSTEXPR /* implicit */ Optional(const Value& newValue) noexcept(
constexpr /* implicit */ Optional(const Value& newValue) noexcept(
std::is_nothrow_copy_constructible<Value>::value) {
construct(newValue);
}
......@@ -153,12 +153,12 @@ class Optional {
Null>::type) noexcept = delete;
template <typename... Args>
FOLLY_CPP14_CONSTEXPR explicit Optional(in_place_t, Args&&... args) noexcept(
constexpr explicit Optional(in_place_t, Args&&... args) noexcept(
std::is_nothrow_constructible<Value, Args...>::value)
: Optional{PrivateConstructor{}, std::forward<Args>(args)...} {}
template <typename U, typename... Args>
FOLLY_CPP14_CONSTEXPR explicit Optional(
constexpr explicit Optional(
in_place_t,
std::initializer_list<U> il,
Args&&... args) noexcept(std::
......@@ -274,22 +274,22 @@ class Optional {
}
}
FOLLY_CPP14_CONSTEXPR const Value& value() const& {
constexpr const Value& value() const& {
require_value();
return storage_.value;
}
FOLLY_CPP14_CONSTEXPR Value& value() & {
constexpr Value& value() & {
require_value();
return storage_.value;
}
FOLLY_CPP14_CONSTEXPR Value&& value() && {
constexpr Value&& value() && {
require_value();
return std::move(storage_.value);
}
FOLLY_CPP14_CONSTEXPR const Value&& value() const&& {
constexpr const Value&& value() const&& {
require_value();
return std::move(storage_.value);
}
......@@ -302,41 +302,41 @@ class Optional {
}
Value* get_pointer() && = delete;
FOLLY_CPP14_CONSTEXPR bool has_value() const noexcept {
constexpr bool has_value() const noexcept {
return storage_.hasValue;
}
FOLLY_CPP14_CONSTEXPR bool hasValue() const noexcept {
constexpr bool hasValue() const noexcept {
return has_value();
}
FOLLY_CPP14_CONSTEXPR explicit operator bool() const noexcept {
constexpr explicit operator bool() const noexcept {
return has_value();
}
FOLLY_CPP14_CONSTEXPR const Value& operator*() const& {
constexpr const Value& operator*() const& {
return value();
}
FOLLY_CPP14_CONSTEXPR Value& operator*() & {
constexpr Value& operator*() & {
return value();
}
FOLLY_CPP14_CONSTEXPR const Value&& operator*() const&& {
constexpr const Value&& operator*() const&& {
return std::move(value());
}
FOLLY_CPP14_CONSTEXPR Value&& operator*() && {
constexpr Value&& operator*() && {
return std::move(value());
}
FOLLY_CPP14_CONSTEXPR const Value* operator->() const {
constexpr const Value* operator->() const {
return &value();
}
FOLLY_CPP14_CONSTEXPR Value* operator->() {
constexpr Value* operator->() {
return &value();
}
// Return a copy of the value if set, or a given default if not.
template <class U>
FOLLY_CPP14_CONSTEXPR Value value_or(U&& dflt) const& {
constexpr Value value_or(U&& dflt) const& {
if (storage_.hasValue) {
return storage_.value;
}
......@@ -345,7 +345,7 @@ class Optional {
}
template <class U>
FOLLY_CPP14_CONSTEXPR Value value_or(U&& dflt) && {
constexpr Value value_or(U&& dflt) && {
if (storage_.hasValue) {
return std::move(storage_.value);
}
......@@ -373,7 +373,7 @@ class Optional {
explicit PrivateConstructor() = default;
};
template <typename... Args>
FOLLY_CPP14_CONSTEXPR Optional(PrivateConstructor, Args&&... args) noexcept(
constexpr Optional(PrivateConstructor, Args&&... args) noexcept(
std::is_constructible<Value, Args&&...>::value) {
construct(std::forward<Args>(args)...);
}
......@@ -493,9 +493,7 @@ constexpr bool operator!=(const U& a, const Optional<V>& b) {
}
template <class U, class V>
FOLLY_CPP14_CONSTEXPR bool operator==(
const Optional<U>& a,
const Optional<V>& b) {
constexpr bool operator==(const Optional<U>& a, const Optional<V>& b) {
if (a.hasValue() != b.hasValue()) {
return false;
}
......@@ -511,9 +509,7 @@ constexpr bool operator!=(const Optional<U>& a, const Optional<V>& b) {
}
template <class U, class V>
FOLLY_CPP14_CONSTEXPR bool operator<(
const Optional<U>& a,
const Optional<V>& b) {
constexpr bool operator<(const Optional<U>& a, const Optional<V>& b) {
if (a.hasValue() != b.hasValue()) {
return a.hasValue() < b.hasValue();
}
......
......@@ -440,24 +440,6 @@ constexpr auto kCpplibVer = 0;
#endif
} // namespace folly
// 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
// MSVC does not permit:
//
// extern int const num;
......@@ -471,19 +453,9 @@ constexpr auto kCpplibVer = 0;
// True as of MSVC 2017.
#if _MSC_VER
#define FOLLY_STORAGE_CONSTEXPR
#define FOLLY_STORAGE_CPP14_CONSTEXPR
#else
#if __ICC
#define FOLLY_STORAGE_CONSTEXPR
#else
#define FOLLY_STORAGE_CONSTEXPR constexpr
#endif
#if FOLLY_USE_CPP14_CONSTEXPR
#define FOLLY_STORAGE_CPP14_CONSTEXPR constexpr
#else
#define FOLLY_STORAGE_CPP14_CONSTEXPR
#endif
#endif
#if __cpp_coroutines >= 201703L && __has_include(<experimental/coroutine>)
#define FOLLY_HAS_COROUTINES 1
......
......@@ -439,7 +439,7 @@ class alignas(T) Replaceable
template <
class... Args,
std::enable_if_t<std::is_constructible<T, Args&&...>::value, int> = 0>
FOLLY_CPP14_CONSTEXPR explicit Replaceable(in_place_t, Args&&... args)
constexpr explicit Replaceable(in_place_t, Args&&... args)
// clang-format off
noexcept(std::is_nothrow_constructible<T, Args&&...>::value)
// clang-format on
......@@ -453,7 +453,7 @@ class alignas(T) Replaceable
std::enable_if_t<
std::is_constructible<T, std::initializer_list<U>, Args&&...>::value,
int> = 0>
FOLLY_CPP14_CONSTEXPR explicit Replaceable(
constexpr explicit Replaceable(
in_place_t,
std::initializer_list<U> il,
Args&&... args)
......@@ -475,7 +475,7 @@ class alignas(T) Replaceable
!std::is_same<Replaceable<T>, std::decay_t<U>>::value &&
std::is_convertible<U&&, T>::value,
int> = 0>
FOLLY_CPP14_CONSTEXPR /* implicit */ Replaceable(U&& other)
constexpr /* implicit */ Replaceable(U&& other)
// clang-format off
noexcept(std::is_nothrow_constructible<T, U&&>::value)
// clang-format on
......@@ -491,7 +491,7 @@ class alignas(T) Replaceable
!std::is_same<Replaceable<T>, std::decay_t<U>>::value &&
!std::is_convertible<U&&, T>::value,
int> = 0>
FOLLY_CPP14_CONSTEXPR explicit Replaceable(U&& other)
constexpr explicit Replaceable(U&& other)
// clang-format off
noexcept(std::is_nothrow_constructible<T, U&&>::value)
// clang-format on
......@@ -611,7 +611,7 @@ class alignas(T) Replaceable
return launder(reinterpret_cast<T const*>(storage_));
}
FOLLY_CPP14_CONSTEXPR T* operator->() {
constexpr T* operator->() {
return launder(reinterpret_cast<T*>(storage_));
}
......@@ -619,11 +619,11 @@ class alignas(T) Replaceable
return *launder(reinterpret_cast<T const*>(storage_));
}
FOLLY_CPP14_CONSTEXPR T& operator*() & {
constexpr T& operator*() & {
return *launder(reinterpret_cast<T*>(storage_));
}
FOLLY_CPP14_CONSTEXPR T&& operator*() && {
constexpr T&& operator*() && {
return std::move(*launder(reinterpret_cast<T*>(storage_)));
}
......
......@@ -313,7 +313,7 @@ decltype(auto) fetch_impl(RangeTag, Sequence&& sequence, Index&& index) {
} // namespace for_each_detail
template <typename Sequence, typename Func>
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence&& sequence, Func func) {
constexpr Func for_each(Sequence&& sequence, Func func) {
namespace fed = for_each_detail;
using tag = fed::SequenceTag<Sequence>;
fed::for_each_impl(tag{}, std::forward<Sequence>(sequence), func);
......@@ -321,7 +321,7 @@ FOLLY_CPP14_CONSTEXPR Func for_each(Sequence&& sequence, Func func) {
}
template <typename Sequence, typename Index>
FOLLY_CPP14_CONSTEXPR decltype(auto) fetch(Sequence&& sequence, Index&& index) {
constexpr decltype(auto) fetch(Sequence&& sequence, Index&& index) {
namespace fed = for_each_detail;
using tag = fed::SequenceTag<Sequence>;
return for_each_detail::fetch_impl(
......
......@@ -83,7 +83,7 @@ namespace folly {
* });
*/
template <typename Range, typename Func>
FOLLY_CPP14_CONSTEXPR Func for_each(Range&& range, Func func);
constexpr Func for_each(Range&& range, Func func);
/**
* The user should return loop_break and loop_continue if they want to iterate
......@@ -119,7 +119,7 @@ constexpr auto loop_continue = for_each_detail::LoopControl::CONTINUE;
* required element.
*/
template <typename Sequence, typename Index>
FOLLY_CPP14_CONSTEXPR decltype(auto) fetch(Sequence&& sequence, Index&& index);
constexpr decltype(auto) fetch(Sequence&& sequence, Index&& index);
} // namespace folly
......
......@@ -64,7 +64,7 @@ class propagate_const {
std::remove_reference_t<decltype(*std::declval<Pointer&>())>;
constexpr propagate_const() = default;
FOLLY_CPP14_CONSTEXPR propagate_const(propagate_const&&) = default;
constexpr propagate_const(propagate_const&&) = default;
propagate_const(propagate_const const&) = delete;
template <
......@@ -105,15 +105,14 @@ class propagate_const {
constexpr propagate_const(OtherPointer&& other)
: pointer_(static_cast<OtherPointer&&>(other)) {}
FOLLY_CPP14_CONSTEXPR propagate_const& operator=(propagate_const&&) = default;
constexpr propagate_const& operator=(propagate_const&&) = default;
propagate_const& operator=(propagate_const const&) = delete;
template <
typename OtherPointer,
typename =
std::enable_if_t<std::is_convertible<OtherPointer&&, Pointer>::value>>
FOLLY_CPP14_CONSTEXPR propagate_const& operator=(
propagate_const<OtherPointer>&& other) {
constexpr propagate_const& operator=(propagate_const<OtherPointer>&& other) {
pointer_ = static_cast<OtherPointer&&>(other.pointer_);
}
......@@ -122,19 +121,19 @@ class propagate_const {
typename = std::enable_if_t<
!detail::is_decay_propagate_const<OtherPointer>::value &&
std::is_convertible<OtherPointer&&, Pointer>::value>>
FOLLY_CPP14_CONSTEXPR propagate_const& operator=(OtherPointer&& other) {
constexpr propagate_const& operator=(OtherPointer&& other) {
pointer_ = static_cast<OtherPointer&&>(other);
return *this;
}
FOLLY_CPP14_CONSTEXPR void swap(propagate_const& other) noexcept(
constexpr void swap(propagate_const& other) noexcept(
noexcept(detail::propagate_const_adl::adl_swap(
std::declval<Pointer&>(),
other.pointer_))) {
detail::propagate_const_adl::adl_swap(pointer_, other.pointer_);
}
FOLLY_CPP14_CONSTEXPR element_type* get() {
constexpr element_type* get() {
return get_(pointer_);
}
......@@ -146,7 +145,7 @@ class propagate_const {
return static_cast<bool>(pointer_);
}
FOLLY_CPP14_CONSTEXPR element_type& operator*() {
constexpr element_type& operator*() {
return *get();
}
......@@ -154,7 +153,7 @@ class propagate_const {
return *get();
}
FOLLY_CPP14_CONSTEXPR element_type* operator->() {
constexpr element_type* operator->() {
return get();
}
......@@ -167,7 +166,7 @@ class propagate_const {
typename = std::enable_if_t<
std::is_pointer<OtherPointer>::value ||
std::is_convertible<OtherPointer, element_type*>::value>>
FOLLY_CPP14_CONSTEXPR operator element_type*() {
constexpr operator element_type*() {
return get();
}
......@@ -199,7 +198,7 @@ class propagate_const {
};
template <typename Pointer>
FOLLY_CPP14_CONSTEXPR void swap(
constexpr void swap(
propagate_const<Pointer>& a,
propagate_const<Pointer>& b) noexcept(noexcept(a.swap(b))) {
a.swap(b);
......
......@@ -159,7 +159,6 @@ TEST(FixedStringConcatTest, FromTwoStrings) {
static_assert(res == "hello world!!!", "");
}
#if FOLLY_USE_CPP14_CONSTEXPR
constexpr folly::FixedString<20> constexpr_swap_test() {
folly::FixedString<10> tmp1{"hello"}, tmp2{"world!"};
tmp2.swap(tmp1);
......@@ -169,7 +168,6 @@ constexpr folly::FixedString<20> constexpr_swap_test() {
TEST(FixedStringSwapTest, ConstexprSwap) {
static_assert(constexpr_swap_test() == "world!hello", "");
}
#endif
TEST(FixedStringSwapTest, RuntimeSwap) {
folly::FixedString<10> tmp1{"hello"}, tmp2{"world!"};
......@@ -177,7 +175,6 @@ TEST(FixedStringSwapTest, RuntimeSwap) {
EXPECT_STREQ((tmp1 + tmp2).c_str(), "world!hello");
}
#if FOLLY_USE_CPP14_CONSTEXPR
constexpr folly::FixedString<10> constexpr_assign_string_test_1() {
folly::FixedString<10> tmp1, tmp2{"world!"};
tmp1 = tmp2;
......@@ -205,7 +202,6 @@ TEST(FixedStringAssignTest, ConstexprAssignString) {
static_assert(constexpr_assign_string_test_3() == "db", "");
static_assert(constexpr_assign_string_test_4() == "dbye", "");
}
#endif
TEST(FixedStringAssignTest, RuntimeAssignString) {
folly::FixedString<10> tmp1, tmp2{"world!"};
......@@ -219,7 +215,6 @@ TEST(FixedStringAssignTest, RuntimeAssignString) {
EXPECT_STREQ("dby", tmp1.c_str());
}
#if FOLLY_USE_CPP14_CONSTEXPR
constexpr folly::FixedString<10> constexpr_assign_literal_test_1() {
folly::FixedString<10> tmp{"aaaaaaaaaa"};
tmp = "hello";
......@@ -244,7 +239,6 @@ TEST(FixedStringAssignTest, ConstexprAssignLiteral) {
static_assert(constexpr_assign_literal_test_2() == "hello", "");
static_assert(constexpr_assign_literal_test_3() == "good", "");
}
#endif
TEST(FixedStringAssignTest, RuntimeAssignLiteral) {
folly::FixedString<10> tmp{"aaaaaaaaaa"};
......@@ -333,7 +327,6 @@ TEST(FixedStringCompareTest, CompareStdString) {
EXPECT_TRUE(tmp2 >= tmp1);
}
#if FOLLY_USE_CPP14_CONSTEXPR
constexpr folly::FixedString<20> constexpr_append_string_test() {
folly::FixedString<20> a{"hello"}, b{"X world!"};
a.append(1u, ' ');
......@@ -345,7 +338,6 @@ constexpr folly::FixedString<20> constexpr_append_string_test() {
TEST(FixedStringAssignTest, ConstexprAppendString) {
static_assert(constexpr_append_string_test() == "hello world!", "");
}
#endif
TEST(FixedStringAssignTest, RuntimeAppendString) {
folly::FixedString<20> a{"hello"}, b{"X world!"};
......@@ -355,7 +347,6 @@ TEST(FixedStringAssignTest, RuntimeAppendString) {
EXPECT_STREQ("hello world!", a.c_str());
}
#if FOLLY_USE_CPP14_CONSTEXPR
constexpr folly::FixedString<20> constexpr_append_literal_test() {
folly::FixedString<20> a{"hello"};
a.append(1u, ' ');
......@@ -367,7 +358,6 @@ constexpr folly::FixedString<20> constexpr_append_literal_test() {
TEST(FixedStringAssignTest, ConstexprAppendLiteral) {
static_assert(constexpr_append_literal_test() == "hello world!", "");
}
#endif
TEST(FixedStringAssignTest, RuntimeAppendLiteral) {
folly::FixedString<20> a{"hello"};
......@@ -393,7 +383,6 @@ TEST(FixedStringCAppendTest, CAppendLiteral) {
static_assert(tmp3 == "hello world!", "");
}
#if FOLLY_USE_CPP14_CONSTEXPR
constexpr folly::FixedString<10> constexpr_replace_string_test() {
folly::FixedString<10> tmp{"abcdefghij"};
tmp.replace(1, 5, FS("XX"));
......@@ -404,7 +393,6 @@ TEST(FixedStringReplaceTest, ConstexprReplaceString) {
static_assert(constexpr_replace_string_test().size() == 7u, "");
static_assert(constexpr_replace_string_test() == "aXXghij", "");
}
#endif
TEST(FixedStringReplaceTest, RuntimeReplaceString) {
folly::FixedString<10> tmp{"abcdefghij"};
......@@ -637,7 +625,6 @@ TEST(FixedStringConversionTest, ConversionToStdString) {
EXPECT_STREQ("another string", str.c_str());
}
#if FOLLY_USE_CPP14_CONSTEXPR
constexpr std::size_t countSpacesReverse(folly::FixedString<50> s) {
std::size_t count = 0u;
auto i = s.rbegin();
......@@ -652,7 +639,6 @@ constexpr std::size_t countSpacesReverse(folly::FixedString<50> s) {
TEST(FixedStringReverseIteratorTest, Cpp14ConstexprReverseIteration) {
static_assert(3 == countSpacesReverse("This is a string"), "");
}
#endif
TEST(FixedStringReverseIteratorTest, ConstexprReverseIteration) {
static constexpr auto alpha = FS("abcdefghijklmnopqrstuvwxyz");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment