Skip to content

Commit

Permalink
[libc++] Remove redundant __invoke_constexpr functions
Browse files Browse the repository at this point in the history
There are `constexpr` versions for the different `__invoke` functions, which seem to be identical other than begin `constexpr` since C++11 instead of being `constexpr` since C++20.

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D123003
  • Loading branch information
philnik777 committed Apr 7, 2022
1 parent 82d0f7b commit 1b9c5f6
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 184 deletions.
2 changes: 1 addition & 1 deletion libcxx/include/tuple
Expand Up @@ -1609,7 +1609,7 @@ inline _LIBCPP_INLINE_VISIBILITY
constexpr decltype(auto) __apply_tuple_impl(_Fn && __f, _Tuple && __t,
__tuple_indices<_Id...>)
_LIBCPP_NOEXCEPT_RETURN(
_VSTD::__invoke_constexpr(
_VSTD::__invoke(
_VSTD::forward<_Fn>(__f),
_VSTD::get<_Id>(_VSTD::forward<_Tuple>(__t))...)
)
Expand Down
79 changes: 7 additions & 72 deletions libcxx/include/type_traits
Expand Up @@ -3460,139 +3460,74 @@ using __enable_if_bullet6 = typename enable_if
template <class ..._Args>
auto __invoke(__any, _Args&& ...__args) -> __nat;

template <class ..._Args>
auto __invoke_constexpr(__any, _Args&& ...__args) -> __nat;

// bullets 1, 2 and 3

template <class _Fp, class _A0, class ..._Args,
class = __enable_if_bullet1<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR_AFTER_CXX17 auto
constexpr auto
__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
noexcept(noexcept((static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...)))
-> decltype( (static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...))
{ return (static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...); }

template <class _Fp, class _A0, class ..._Args,
class = __enable_if_bullet1<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR auto
__invoke_constexpr(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
noexcept(noexcept((static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...)))
-> decltype( (static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...))
{ return (static_cast<_A0&&>(__a0).*__f)(static_cast<_Args&&>(__args)...); }

template <class _Fp, class _A0, class ..._Args,
class = __enable_if_bullet2<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR_AFTER_CXX17 auto
constexpr auto
__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
noexcept(noexcept((__a0.get().*__f)(static_cast<_Args&&>(__args)...)))
-> decltype( (__a0.get().*__f)(static_cast<_Args&&>(__args)...))
{ return (__a0.get().*__f)(static_cast<_Args&&>(__args)...); }

template <class _Fp, class _A0, class ..._Args,
class = __enable_if_bullet2<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR auto
__invoke_constexpr(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
noexcept(noexcept((__a0.get().*__f)(static_cast<_Args&&>(__args)...)))
-> decltype( (__a0.get().*__f)(static_cast<_Args&&>(__args)...))
{ return (__a0.get().*__f)(static_cast<_Args&&>(__args)...); }

template <class _Fp, class _A0, class ..._Args,
class = __enable_if_bullet3<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR_AFTER_CXX17 auto
constexpr auto
__invoke(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
noexcept(noexcept(((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...)))
-> decltype( ((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...))
{ return ((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...); }

template <class _Fp, class _A0, class ..._Args,
class = __enable_if_bullet3<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR auto
__invoke_constexpr(_Fp&& __f, _A0&& __a0, _Args&& ...__args)
noexcept(noexcept(((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...)))
-> decltype( ((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...))
{ return ((*static_cast<_A0&&>(__a0)).*__f)(static_cast<_Args&&>(__args)...); }

// bullets 4, 5 and 6

template <class _Fp, class _A0,
class = __enable_if_bullet4<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR_AFTER_CXX17 auto
constexpr auto
__invoke(_Fp&& __f, _A0&& __a0)
noexcept(noexcept(static_cast<_A0&&>(__a0).*__f))
-> decltype( static_cast<_A0&&>(__a0).*__f)
{ return static_cast<_A0&&>(__a0).*__f; }

template <class _Fp, class _A0,
class = __enable_if_bullet4<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR auto
__invoke_constexpr(_Fp&& __f, _A0&& __a0)
noexcept(noexcept(static_cast<_A0&&>(__a0).*__f))
-> decltype( static_cast<_A0&&>(__a0).*__f)
{ return static_cast<_A0&&>(__a0).*__f; }

template <class _Fp, class _A0,
class = __enable_if_bullet5<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR_AFTER_CXX17 auto
constexpr auto
__invoke(_Fp&& __f, _A0&& __a0)
noexcept(noexcept(__a0.get().*__f))
-> decltype( __a0.get().*__f)
{ return __a0.get().*__f; }

template <class _Fp, class _A0,
class = __enable_if_bullet5<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR auto
__invoke_constexpr(_Fp&& __f, _A0&& __a0)
noexcept(noexcept(__a0.get().*__f))
-> decltype( __a0.get().*__f)
{ return __a0.get().*__f; }

template <class _Fp, class _A0,
class = __enable_if_bullet6<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR_AFTER_CXX17 auto
constexpr auto
__invoke(_Fp&& __f, _A0&& __a0)
noexcept(noexcept((*static_cast<_A0&&>(__a0)).*__f))
-> decltype( (*static_cast<_A0&&>(__a0)).*__f)
{ return (*static_cast<_A0&&>(__a0)).*__f; }

template <class _Fp, class _A0,
class = __enable_if_bullet6<_Fp, _A0>>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR auto
__invoke_constexpr(_Fp&& __f, _A0&& __a0)
noexcept(noexcept((*static_cast<_A0&&>(__a0)).*__f))
-> decltype( (*static_cast<_A0&&>(__a0)).*__f)
{ return (*static_cast<_A0&&>(__a0)).*__f; }

// bullet 7

template <class _Fp, class ..._Args>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR_AFTER_CXX17 auto
constexpr auto
__invoke(_Fp&& __f, _Args&& ...__args)
noexcept(noexcept(static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...)))
-> decltype( static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...))
{ return static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...); }

template <class _Fp, class ..._Args>
inline _LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR auto
__invoke_constexpr(_Fp&& __f, _Args&& ...__args)
noexcept(noexcept(static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...)))
-> decltype( static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...))
{ return static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...); }

// __invokable
template <class _Ret, class _Fp, class ..._Args>
struct __invokable_r
Expand Down
14 changes: 7 additions & 7 deletions libcxx/include/variant
Expand Up @@ -547,7 +547,7 @@ private:
template <class _Fp, class... _Vs>
inline _LIBCPP_INLINE_VISIBILITY
static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
return _VSTD::__invoke_constexpr(
return _VSTD::__invoke(
static_cast<_Fp>(__f),
__access::__base::__get_alt<_Is>(static_cast<_Vs>(__vs))...);
}
Expand Down Expand Up @@ -667,8 +667,8 @@ private:
__std_visit_exhaustive_visitor_check<
_Visitor,
decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Alts>(__alts).__value...);
return _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Alts>(__alts).__value...);
}
_Visitor&& __visitor;
};
Expand All @@ -683,12 +683,12 @@ private:
_Visitor,
decltype((_VSTD::forward<_Alts>(__alts).__value))...>();
if constexpr (is_void_v<_Rp>) {
_VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Alts>(__alts).__value...);
_VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Alts>(__alts).__value...);
}
else {
return _VSTD::__invoke_constexpr(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Alts>(__alts).__value...);
return _VSTD::__invoke(_VSTD::forward<_Visitor>(__visitor),
_VSTD::forward<_Alts>(__alts).__value...);
}
}

Expand Down
Expand Up @@ -244,30 +244,55 @@ struct TestCase : public TestCaseImp<MemFun03, Sig, Arity, CV> {};
#if TEST_STD_VER >= 11
template <class Sig, int Arity, class CV, bool RValue = false>
struct TestCase11 : public TestCaseImp<MemFun11, Sig, Arity, CV, RValue, true> {};
#endif

template <class Type>
struct ReferenceWrapper {
using type = Type;
Type* ptr;

static void fun(Type&) noexcept;
static void fun(Type&&) = delete;

template <class Type2,
class = typename std::enable_if<!std::__is_same_uncvref<Type2, ReferenceWrapper>::value>::type>
constexpr ReferenceWrapper(Type2&& t) noexcept : ptr(&t) {}

constexpr Type& get() const noexcept { return *ptr; }
constexpr operator Type&() const noexcept { return *ptr; }

template <class... _ArgTypes>
constexpr typename std::__invoke_of<Type&, _ArgTypes...>::type operator() (_ArgTypes&&... __args) const {
return std::__invoke(get(), std::forward<_ArgTypes>(__args)...);
}
};

template <class Tp>
struct DerivedFromRefWrap : public std::reference_wrapper<Tp> {
DerivedFromRefWrap(Tp& tp) : std::reference_wrapper<Tp>(tp) {}
struct DerivedFromRefWrap : public ReferenceWrapper<Tp> {
constexpr DerivedFromRefWrap(Tp& tp) : ReferenceWrapper<Tp>(tp) {}
};

#if TEST_STD_VER >= 11
void test_derived_from_ref_wrap() {
TEST_CONSTEXPR_CXX14 bool test_derived_from_ref_wrap() {
int x = 42;
std::reference_wrapper<int> r(x);
std::reference_wrapper<std::reference_wrapper<int>> r2(r);
ReferenceWrapper<int> r(x);
DerivedFromRefWrap<int> d(x);
auto get_fn = &std::reference_wrapper<int>::get;
auto get_fn = &ReferenceWrapper<int>::get;
auto& ret = std::__invoke(get_fn, r);
auto& cret = std::__invoke_constexpr(get_fn, r);
assert(&ret == &x);
assert(&cret == &x);
auto& ret2 = std::__invoke(get_fn, d);
auto& cret2 = std::__invoke_constexpr(get_fn, d);
assert(&ret2 == &x);
assert(&cret2 == &x);

return true;
}

TEST_CONSTEXPR_CXX20 bool test_reference_wrapper_reference_wrapper() {
int x = 42;
auto get_fn = &std::reference_wrapper<int>::get;
std::reference_wrapper<int> r(x);
std::reference_wrapper<std::reference_wrapper<int>> r2(r);
auto& ret3 = std::__invoke(get_fn, r2);
assert(&ret3 == &x);

return true;
}
#endif

Expand Down Expand Up @@ -366,7 +391,14 @@ int main(int, char**) {
TestCase11<R(A&&, A&&, A&&, ...) const volatile &&, 3, Q_CV, /* RValue */ true>::run();

test_derived_from_ref_wrap();
test_reference_wrapper_reference_wrapper();
#if TEST_STD_VER > 11
static_assert(test_derived_from_ref_wrap(), "");
#endif
#if TEST_STD_VER > 17
static_assert(test_reference_wrapper_reference_wrapper(), "");
#endif
#endif // TEST_STD_VER >= 11

return 0;
}
Expand Up @@ -195,21 +195,9 @@ struct TestCase {
Expect e = std::__invoke(M, std::forward<T>(obj));
assert(&e == expect);
}
#if TEST_STD_VER >= 11
{
static_assert((std::is_same<
decltype(std::__invoke_constexpr(M, std::forward<T>(obj))), Expect
>::value), "");
Expect e = std::__invoke_constexpr(M, std::forward<T>(obj));
assert(&e == expect);
}
#endif
}
};




int main(int, char**) {
TestCase<ArgType>::run();
TestCase<ArgType const>::run();
Expand Down

0 comments on commit 1b9c5f6

Please sign in to comment.