17b00e9faSArthur O'Dwyer //===----------------------------------------------------------------------===//
27b00e9faSArthur O'Dwyer //
37b00e9faSArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47b00e9faSArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
57b00e9faSArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67b00e9faSArthur O'Dwyer //
77b00e9faSArthur O'Dwyer //===----------------------------------------------------------------------===//
87b00e9faSArthur O'Dwyer
97b00e9faSArthur O'Dwyer // UNSUPPORTED: c++03, c++11, c++14, c++17
107b00e9faSArthur O'Dwyer
117b00e9faSArthur O'Dwyer // <functional>
127b00e9faSArthur O'Dwyer
13*2ff5a56eSwmbat // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_TYPE_TRAITS
14*2ff5a56eSwmbat // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
15*2ff5a56eSwmbat
167b00e9faSArthur O'Dwyer // template<class F, class... Args>
177b00e9faSArthur O'Dwyer // constexpr // constexpr in C++20
187b00e9faSArthur O'Dwyer // invoke_result_t<F, Args...> invoke(F&& f, Args&&... args)
197b00e9faSArthur O'Dwyer // noexcept(is_nothrow_invocable_v<_Fn, _Args...>);
207b00e9faSArthur O'Dwyer
217b00e9faSArthur O'Dwyer /// C++14 [func.def] 20.9.0
227b00e9faSArthur O'Dwyer /// (1) The following definitions apply to this Clause:
237b00e9faSArthur O'Dwyer /// (2) A call signature is the name of a return type followed by a parenthesized
247b00e9faSArthur O'Dwyer /// comma-separated list of zero or more argument types.
257b00e9faSArthur O'Dwyer /// (3) A callable type is a function object type (20.9) or a pointer to member.
267b00e9faSArthur O'Dwyer /// (4) A callable object is an object of a callable type.
277b00e9faSArthur O'Dwyer /// (5) A call wrapper type is a type that holds a callable object and supports
287b00e9faSArthur O'Dwyer /// a call operation that forwards to that object.
297b00e9faSArthur O'Dwyer /// (6) A call wrapper is an object of a call wrapper type.
307b00e9faSArthur O'Dwyer /// (7) A target object is the callable object held by a call wrapper.
317b00e9faSArthur O'Dwyer
327b00e9faSArthur O'Dwyer /// C++14 [func.require] 20.9.1
337b00e9faSArthur O'Dwyer ///
347b00e9faSArthur O'Dwyer /// Define INVOKE (f, t1, t2, ..., tN) as follows:
357b00e9faSArthur O'Dwyer /// (1.1) - (t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is an object of
367b00e9faSArthur O'Dwyer /// type T or a reference to an object of type T or a reference to an object of a type derived from T;
377b00e9faSArthur O'Dwyer /// (1.2) - ((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is not one of
387b00e9faSArthur O'Dwyer /// the types described in the previous item;
397b00e9faSArthur O'Dwyer /// (1.3) - t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is an object of type T or a
407b00e9faSArthur O'Dwyer /// reference to an object of type T or a reference to an object of a type derived from T;
417b00e9faSArthur O'Dwyer /// (1.4) - (*t1).*f when N == 1 and f is a pointer to member data of a class T and t1 is not one of the types
427b00e9faSArthur O'Dwyer /// described in the previous item;
437b00e9faSArthur O'Dwyer /// (1.5) - f(t1, t2, ..., tN) in all other cases.
447b00e9faSArthur O'Dwyer
457b00e9faSArthur O'Dwyer #include <functional>
467b00e9faSArthur O'Dwyer #include <type_traits>
477b00e9faSArthur O'Dwyer #include <utility> // for std::move
487b00e9faSArthur O'Dwyer #include <cassert>
497b00e9faSArthur O'Dwyer
507b00e9faSArthur O'Dwyer #include "test_macros.h"
517b00e9faSArthur O'Dwyer
527b00e9faSArthur O'Dwyer struct NonCopyable {
NonCopyableNonCopyable537b00e9faSArthur O'Dwyer constexpr NonCopyable() {}
547b00e9faSArthur O'Dwyer private:
557b00e9faSArthur O'Dwyer NonCopyable(NonCopyable const&) = delete;
567b00e9faSArthur O'Dwyer NonCopyable& operator=(NonCopyable const&) = delete;
577b00e9faSArthur O'Dwyer };
587b00e9faSArthur O'Dwyer
597b00e9faSArthur O'Dwyer struct TestClass {
TestClassTestClass607b00e9faSArthur O'Dwyer constexpr explicit TestClass(int x) : data(x) {}
617b00e9faSArthur O'Dwyer
operator ()TestClass627b00e9faSArthur O'Dwyer constexpr int& operator()(NonCopyable&&) & { return data; }
operator ()TestClass637b00e9faSArthur O'Dwyer constexpr int const& operator()(NonCopyable&&) const & { return data; }
647b00e9faSArthur O'Dwyer
operator ()TestClass657b00e9faSArthur O'Dwyer constexpr int&& operator()(NonCopyable&&) && { return std::move(data); }
operator ()TestClass667b00e9faSArthur O'Dwyer constexpr int const&& operator()(NonCopyable&&) const && { return std::move(data); }
677b00e9faSArthur O'Dwyer
687b00e9faSArthur O'Dwyer int data;
697b00e9faSArthur O'Dwyer private:
707b00e9faSArthur O'Dwyer TestClass(TestClass const&) = delete;
717b00e9faSArthur O'Dwyer TestClass& operator=(TestClass const&) = delete;
727b00e9faSArthur O'Dwyer };
737b00e9faSArthur O'Dwyer
747b00e9faSArthur O'Dwyer struct DerivedFromTestClass : public TestClass {
DerivedFromTestClassDerivedFromTestClass757b00e9faSArthur O'Dwyer constexpr explicit DerivedFromTestClass(int x) : TestClass(x) {}
767b00e9faSArthur O'Dwyer };
777b00e9faSArthur O'Dwyer
787b00e9faSArthur O'Dwyer static constexpr int data = 42;
foo(NonCopyable &&)797b00e9faSArthur O'Dwyer constexpr const int& foo(NonCopyable&&) {
807b00e9faSArthur O'Dwyer return data;
817b00e9faSArthur O'Dwyer }
827b00e9faSArthur O'Dwyer
837b00e9faSArthur O'Dwyer template <class Signature, class Expect, class Functor>
test_b12(Functor && f)847b00e9faSArthur O'Dwyer constexpr void test_b12(Functor&& f) {
857b00e9faSArthur O'Dwyer // Create the callable object.
867b00e9faSArthur O'Dwyer typedef Signature TestClass::*ClassFunc;
877b00e9faSArthur O'Dwyer ClassFunc func_ptr = &TestClass::operator();
887b00e9faSArthur O'Dwyer
897b00e9faSArthur O'Dwyer // Create the dummy arg.
907b00e9faSArthur O'Dwyer NonCopyable arg;
917b00e9faSArthur O'Dwyer
927b00e9faSArthur O'Dwyer // Check that the deduced return type of invoke is what is expected.
937b00e9faSArthur O'Dwyer typedef decltype(
947b00e9faSArthur O'Dwyer std::invoke(func_ptr, std::forward<Functor>(f), std::move(arg))
957b00e9faSArthur O'Dwyer ) DeducedReturnType;
967b00e9faSArthur O'Dwyer static_assert((std::is_same<DeducedReturnType, Expect>::value), "");
977b00e9faSArthur O'Dwyer
987b00e9faSArthur O'Dwyer // Check that result_of_t matches Expect.
997b00e9faSArthur O'Dwyer typedef typename std::result_of<ClassFunc&&(Functor&&, NonCopyable&&)>::type
1007b00e9faSArthur O'Dwyer ResultOfReturnType;
1017b00e9faSArthur O'Dwyer static_assert((std::is_same<ResultOfReturnType, Expect>::value), "");
1027b00e9faSArthur O'Dwyer
1037b00e9faSArthur O'Dwyer // Run invoke and check the return value.
1047b00e9faSArthur O'Dwyer DeducedReturnType ret =
1057b00e9faSArthur O'Dwyer std::invoke(func_ptr, std::forward<Functor>(f), std::move(arg));
1067b00e9faSArthur O'Dwyer assert(ret == 42);
1077b00e9faSArthur O'Dwyer }
1087b00e9faSArthur O'Dwyer
1097b00e9faSArthur O'Dwyer template <class Expect, class Functor>
test_b34(Functor && f)1107b00e9faSArthur O'Dwyer constexpr void test_b34(Functor&& f) {
1117b00e9faSArthur O'Dwyer // Create the callable object.
1127b00e9faSArthur O'Dwyer typedef int TestClass::*ClassFunc;
1137b00e9faSArthur O'Dwyer ClassFunc func_ptr = &TestClass::data;
1147b00e9faSArthur O'Dwyer
1157b00e9faSArthur O'Dwyer // Check that the deduced return type of invoke is what is expected.
1167b00e9faSArthur O'Dwyer typedef decltype(
1177b00e9faSArthur O'Dwyer std::invoke(func_ptr, std::forward<Functor>(f))
1187b00e9faSArthur O'Dwyer ) DeducedReturnType;
1197b00e9faSArthur O'Dwyer static_assert((std::is_same<DeducedReturnType, Expect>::value), "");
1207b00e9faSArthur O'Dwyer
1217b00e9faSArthur O'Dwyer // Check that result_of_t matches Expect.
1227b00e9faSArthur O'Dwyer typedef typename std::result_of<ClassFunc&&(Functor&&)>::type
1237b00e9faSArthur O'Dwyer ResultOfReturnType;
1247b00e9faSArthur O'Dwyer static_assert((std::is_same<ResultOfReturnType, Expect>::value), "");
1257b00e9faSArthur O'Dwyer
1267b00e9faSArthur O'Dwyer // Run invoke and check the return value.
1277b00e9faSArthur O'Dwyer DeducedReturnType ret =
1287b00e9faSArthur O'Dwyer std::invoke(func_ptr, std::forward<Functor>(f));
1297b00e9faSArthur O'Dwyer assert(ret == 42);
1307b00e9faSArthur O'Dwyer }
1317b00e9faSArthur O'Dwyer
1327b00e9faSArthur O'Dwyer template <class Expect, class Functor>
test_b5(Functor && f)1337b00e9faSArthur O'Dwyer constexpr void test_b5(Functor&& f) {
1347b00e9faSArthur O'Dwyer NonCopyable arg;
1357b00e9faSArthur O'Dwyer
1367b00e9faSArthur O'Dwyer // Check that the deduced return type of invoke is what is expected.
1377b00e9faSArthur O'Dwyer typedef decltype(
1387b00e9faSArthur O'Dwyer std::invoke(std::forward<Functor>(f), std::move(arg))
1397b00e9faSArthur O'Dwyer ) DeducedReturnType;
1407b00e9faSArthur O'Dwyer static_assert((std::is_same<DeducedReturnType, Expect>::value), "");
1417b00e9faSArthur O'Dwyer
1427b00e9faSArthur O'Dwyer // Check that result_of_t matches Expect.
1437b00e9faSArthur O'Dwyer typedef typename std::result_of<Functor&&(NonCopyable&&)>::type
1447b00e9faSArthur O'Dwyer ResultOfReturnType;
1457b00e9faSArthur O'Dwyer static_assert((std::is_same<ResultOfReturnType, Expect>::value), "");
1467b00e9faSArthur O'Dwyer
1477b00e9faSArthur O'Dwyer // Run invoke and check the return value.
1487b00e9faSArthur O'Dwyer DeducedReturnType ret = std::invoke(std::forward<Functor>(f), std::move(arg));
1497b00e9faSArthur O'Dwyer assert(ret == 42);
1507b00e9faSArthur O'Dwyer }
1517b00e9faSArthur O'Dwyer
bullet_one_two_tests()1527b00e9faSArthur O'Dwyer constexpr bool bullet_one_two_tests() {
1537b00e9faSArthur O'Dwyer {
1547b00e9faSArthur O'Dwyer TestClass cl(42);
1557b00e9faSArthur O'Dwyer test_b12<int&(NonCopyable&&) &, int&>(cl);
1567b00e9faSArthur O'Dwyer test_b12<int const&(NonCopyable&&) const &, int const&>(cl);
1577b00e9faSArthur O'Dwyer
1587b00e9faSArthur O'Dwyer test_b12<int&&(NonCopyable&&) &&, int&&>(std::move(cl));
1597b00e9faSArthur O'Dwyer test_b12<int const&&(NonCopyable&&) const &&, int const&&>(std::move(cl));
1607b00e9faSArthur O'Dwyer }
1617b00e9faSArthur O'Dwyer {
1627b00e9faSArthur O'Dwyer DerivedFromTestClass cl(42);
1637b00e9faSArthur O'Dwyer test_b12<int&(NonCopyable&&) &, int&>(cl);
1647b00e9faSArthur O'Dwyer test_b12<int const&(NonCopyable&&) const &, int const&>(cl);
1657b00e9faSArthur O'Dwyer
1667b00e9faSArthur O'Dwyer test_b12<int&&(NonCopyable&&) &&, int&&>(std::move(cl));
1677b00e9faSArthur O'Dwyer test_b12<int const&&(NonCopyable&&) const &&, int const&&>(std::move(cl));
1687b00e9faSArthur O'Dwyer }
1697b00e9faSArthur O'Dwyer {
1707b00e9faSArthur O'Dwyer TestClass cl_obj(42);
1717b00e9faSArthur O'Dwyer std::reference_wrapper<TestClass> cl(cl_obj);
1727b00e9faSArthur O'Dwyer test_b12<int&(NonCopyable&&) &, int&>(cl);
1737b00e9faSArthur O'Dwyer test_b12<int const&(NonCopyable&&) const &, int const&>(cl);
1747b00e9faSArthur O'Dwyer
1757b00e9faSArthur O'Dwyer test_b12<int&(NonCopyable&&) &, int&>(std::move(cl));
1767b00e9faSArthur O'Dwyer test_b12<int const&(NonCopyable&&) const &, int const&>(std::move(cl));
1777b00e9faSArthur O'Dwyer }
1787b00e9faSArthur O'Dwyer {
1797b00e9faSArthur O'Dwyer DerivedFromTestClass cl_obj(42);
1807b00e9faSArthur O'Dwyer std::reference_wrapper<DerivedFromTestClass> cl(cl_obj);
1817b00e9faSArthur O'Dwyer test_b12<int&(NonCopyable&&) &, int&>(cl);
1827b00e9faSArthur O'Dwyer test_b12<int const&(NonCopyable&&) const &, int const&>(cl);
1837b00e9faSArthur O'Dwyer
1847b00e9faSArthur O'Dwyer test_b12<int&(NonCopyable&&) &, int&>(std::move(cl));
1857b00e9faSArthur O'Dwyer test_b12<int const&(NonCopyable&&) const &, int const&>(std::move(cl));
1867b00e9faSArthur O'Dwyer }
1877b00e9faSArthur O'Dwyer {
1887b00e9faSArthur O'Dwyer TestClass cl_obj(42);
1897b00e9faSArthur O'Dwyer TestClass *cl = &cl_obj;
1907b00e9faSArthur O'Dwyer test_b12<int&(NonCopyable&&) &, int&>(cl);
1917b00e9faSArthur O'Dwyer test_b12<int const&(NonCopyable&&) const &, int const&>(cl);
1927b00e9faSArthur O'Dwyer }
1937b00e9faSArthur O'Dwyer {
1947b00e9faSArthur O'Dwyer DerivedFromTestClass cl_obj(42);
1957b00e9faSArthur O'Dwyer DerivedFromTestClass *cl = &cl_obj;
1967b00e9faSArthur O'Dwyer test_b12<int&(NonCopyable&&) &, int&>(cl);
1977b00e9faSArthur O'Dwyer test_b12<int const&(NonCopyable&&) const &, int const&>(cl);
1987b00e9faSArthur O'Dwyer }
1997b00e9faSArthur O'Dwyer return true;
2007b00e9faSArthur O'Dwyer }
2017b00e9faSArthur O'Dwyer
bullet_three_four_tests()2027b00e9faSArthur O'Dwyer constexpr bool bullet_three_four_tests() {
2037b00e9faSArthur O'Dwyer {
2047b00e9faSArthur O'Dwyer typedef TestClass Fn;
2057b00e9faSArthur O'Dwyer Fn cl(42);
2067b00e9faSArthur O'Dwyer test_b34<int&>(cl);
2077b00e9faSArthur O'Dwyer test_b34<int const&>(static_cast<Fn const&>(cl));
2087b00e9faSArthur O'Dwyer
2097b00e9faSArthur O'Dwyer test_b34<int&&>(static_cast<Fn &&>(cl));
2107b00e9faSArthur O'Dwyer test_b34<int const&&>(static_cast<Fn const&&>(cl));
2117b00e9faSArthur O'Dwyer }
2127b00e9faSArthur O'Dwyer {
2137b00e9faSArthur O'Dwyer typedef DerivedFromTestClass Fn;
2147b00e9faSArthur O'Dwyer Fn cl(42);
2157b00e9faSArthur O'Dwyer test_b34<int&>(cl);
2167b00e9faSArthur O'Dwyer test_b34<int const&>(static_cast<Fn const&>(cl));
2177b00e9faSArthur O'Dwyer
2187b00e9faSArthur O'Dwyer test_b34<int&&>(static_cast<Fn &&>(cl));
2197b00e9faSArthur O'Dwyer test_b34<int const&&>(static_cast<Fn const&&>(cl));
2207b00e9faSArthur O'Dwyer }
2217b00e9faSArthur O'Dwyer {
2227b00e9faSArthur O'Dwyer typedef TestClass Fn;
2237b00e9faSArthur O'Dwyer Fn cl(42);
2247b00e9faSArthur O'Dwyer test_b34<int&>(std::reference_wrapper<Fn>(cl));
2257b00e9faSArthur O'Dwyer test_b34<int const&>(std::reference_wrapper<Fn const>(cl));
2267b00e9faSArthur O'Dwyer }
2277b00e9faSArthur O'Dwyer {
2287b00e9faSArthur O'Dwyer typedef DerivedFromTestClass Fn;
2297b00e9faSArthur O'Dwyer Fn cl(42);
2307b00e9faSArthur O'Dwyer test_b34<int&>(std::reference_wrapper<Fn>(cl));
2317b00e9faSArthur O'Dwyer test_b34<int const&>(std::reference_wrapper<Fn const>(cl));
2327b00e9faSArthur O'Dwyer }
2337b00e9faSArthur O'Dwyer {
2347b00e9faSArthur O'Dwyer typedef TestClass Fn;
2357b00e9faSArthur O'Dwyer Fn cl_obj(42);
2367b00e9faSArthur O'Dwyer Fn* cl = &cl_obj;
2377b00e9faSArthur O'Dwyer test_b34<int&>(cl);
2387b00e9faSArthur O'Dwyer test_b34<int const&>(static_cast<Fn const*>(cl));
2397b00e9faSArthur O'Dwyer }
2407b00e9faSArthur O'Dwyer {
2417b00e9faSArthur O'Dwyer typedef DerivedFromTestClass Fn;
2427b00e9faSArthur O'Dwyer Fn cl_obj(42);
2437b00e9faSArthur O'Dwyer Fn* cl = &cl_obj;
2447b00e9faSArthur O'Dwyer test_b34<int&>(cl);
2457b00e9faSArthur O'Dwyer test_b34<int const&>(static_cast<Fn const*>(cl));
2467b00e9faSArthur O'Dwyer }
2477b00e9faSArthur O'Dwyer return true;
2487b00e9faSArthur O'Dwyer }
2497b00e9faSArthur O'Dwyer
bullet_five_tests()2507b00e9faSArthur O'Dwyer constexpr bool bullet_five_tests() {
2517b00e9faSArthur O'Dwyer using FooType = const int&(NonCopyable&&);
2527b00e9faSArthur O'Dwyer {
2537b00e9faSArthur O'Dwyer FooType& fn = foo;
2547b00e9faSArthur O'Dwyer test_b5<const int &>(fn);
2557b00e9faSArthur O'Dwyer }
2567b00e9faSArthur O'Dwyer {
2577b00e9faSArthur O'Dwyer FooType* fn = foo;
2587b00e9faSArthur O'Dwyer test_b5<const int &>(fn);
2597b00e9faSArthur O'Dwyer }
2607b00e9faSArthur O'Dwyer {
2617b00e9faSArthur O'Dwyer typedef TestClass Fn;
2627b00e9faSArthur O'Dwyer Fn cl(42);
2637b00e9faSArthur O'Dwyer test_b5<int&>(cl);
2647b00e9faSArthur O'Dwyer test_b5<int const&>(static_cast<Fn const&>(cl));
2657b00e9faSArthur O'Dwyer
2667b00e9faSArthur O'Dwyer test_b5<int&&>(static_cast<Fn &&>(cl));
2677b00e9faSArthur O'Dwyer test_b5<int const&&>(static_cast<Fn const&&>(cl));
2687b00e9faSArthur O'Dwyer }
2697b00e9faSArthur O'Dwyer return true;
2707b00e9faSArthur O'Dwyer }
2717b00e9faSArthur O'Dwyer
main(int,char **)2727b00e9faSArthur O'Dwyer int main(int, char**) {
2737b00e9faSArthur O'Dwyer bullet_one_two_tests();
2747b00e9faSArthur O'Dwyer bullet_three_four_tests();
2757b00e9faSArthur O'Dwyer bullet_five_tests();
2767b00e9faSArthur O'Dwyer
2777b00e9faSArthur O'Dwyer static_assert(bullet_one_two_tests());
2787b00e9faSArthur O'Dwyer static_assert(bullet_three_four_tests());
2797b00e9faSArthur O'Dwyer static_assert(bullet_five_tests());
2807b00e9faSArthur O'Dwyer
2817b00e9faSArthur O'Dwyer return 0;
2827b00e9faSArthur O'Dwyer }
283