Index: include/functional =================================================================== --- include/functional +++ include/functional @@ -2447,6 +2447,15 @@ }; #endif + +#if _LIBCPP_STD_VER > 14 +template +result_of_t<_Fn&&(_Args&&...)> +invoke(_Fn&& __f, _Args&&... __args) { + return __invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...); +} +#endif + // struct hash in _LIBCPP_END_NAMESPACE_STD Index: test/std/utilities/function.objects/func.invoke/invoke.pass.cpp =================================================================== --- /dev/null +++ test/std/utilities/function.objects/func.invoke/invoke.pass.cpp @@ -0,0 +1,268 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11 + +// + +// template +// result_of_t invoke(F&&, Args&&...); + +/// C++14 [func.def] 20.9.0 +/// (1) The following definitions apply to this Clause: +/// (2) A call signature is the name of a return type followed by a parenthesized +/// comma-separated list of zero or more argument types. +/// (3) A callable type is a function object type (20.9) or a pointer to member. +/// (4) A callable object is an object of a callable type. +/// (5) A call wrapper type is a type that holds a callable object and supports +/// a call operation that forwards to that object. +/// (6) A call wrapper is an object of a call wrapper type. +/// (7) A target object is the callable object held by a call wrapper. + +/// C++14 [func.require] 20.9.1 +/// +/// Define INVOKE (f, t1, t2, ..., tN) as follows: +/// (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 +/// type T or a reference to an object of type T or a reference to an object of a type derived from T; +/// (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 +/// the types described in the previous item; +/// (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 +/// reference to an object of type T or a reference to an object of a type derived from T; +/// (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 +/// described in the previous item; +/// (1.5) — f(t1, t2, ..., tN) in all other cases. + +#include +#include +#include + +struct NonCopyable { + NonCopyable() {} +private: + NonCopyable(NonCopyable const&) = delete; + NonCopyable& operator=(NonCopyable const&) = delete; +}; + +struct TestClass { + explicit TestClass(int x) : data(x) {} + + int& operator()(NonCopyable&&) & { return data; } + int const& operator()(NonCopyable&&) const & { return data; } + int volatile& operator()(NonCopyable&&) volatile & { return data; } + int const volatile& operator()(NonCopyable&&) const volatile & { return data; } + + int&& operator()(NonCopyable&&) && { return std::move(data); } + int const&& operator()(NonCopyable&&) const && { return std::move(data); } + int volatile&& operator()(NonCopyable&&) volatile && { return std::move(data); } + int const volatile&& operator()(NonCopyable&&) const volatile && { return std::move(data); } + + int data; +private: + TestClass(TestClass const&) = delete; + TestClass& operator=(TestClass const&) = delete; +}; + +struct DerivedFromTestClass : public TestClass { + explicit DerivedFromTestClass(int x) : TestClass(x) {} +}; + +int& foo(NonCopyable&&) { + static int data = 42; + return data; +} + +template +void test_b12(Functor&& f) { + // Create the callable object. + typedef Signature TestClass::*ClassFunc; + ClassFunc func_ptr = &TestClass::operator(); + + // Create the dummy arg. + NonCopyable arg; + + // Check that the deduced return type of invoke is what is expected. + typedef decltype( + std::invoke(func_ptr, std::forward(f), std::move(arg)) + ) DeducedReturnType; + static_assert((std::is_same::value), ""); + + // Check that result_of_t matches Expect. + typedef typename std::result_of::type + ResultOfReturnType; + static_assert((std::is_same::value), ""); + + // Run invoke and check the return value. + DeducedReturnType ret = + std::invoke(func_ptr, std::forward(f), std::move(arg)); + assert(ret == 42); +} + +template +void test_b34(Functor&& f) { + // Create the callable object. + typedef int TestClass::*ClassFunc; + ClassFunc func_ptr = &TestClass::data; + + // Check that the deduced return type of invoke is what is expected. + typedef decltype( + std::invoke(func_ptr, std::forward(f)) + ) DeducedReturnType; + static_assert((std::is_same::value), ""); + + // Check that result_of_t matches Expect. + typedef typename std::result_of::type + ResultOfReturnType; + static_assert((std::is_same::value), ""); + + // Run invoke and check the return value. + DeducedReturnType ret = + std::invoke(func_ptr, std::forward(f)); + assert(ret == 42); +} + +template +void test_b5(Functor&& f) { + NonCopyable arg; + + // Check that the deduced return type of invoke is what is expected. + typedef decltype( + std::invoke(std::forward(f), std::move(arg)) + ) DeducedReturnType; + static_assert((std::is_same::value), ""); + + // Check that result_of_t matches Expect. + typedef typename std::result_of::type + ResultOfReturnType; + static_assert((std::is_same::value), ""); + + // Run invoke and check the return value. + DeducedReturnType ret = std::invoke(std::forward(f), std::move(arg)); + assert(ret == 42); +} + +void bullet_one_two_tests() { + { + TestClass cl(42); + test_b12(cl); + test_b12(cl); + test_b12(cl); + test_b12(cl); + + test_b12(std::move(cl)); + test_b12(std::move(cl)); + test_b12(std::move(cl)); + test_b12(std::move(cl)); + } + { + DerivedFromTestClass cl(42); + test_b12(cl); + test_b12(cl); + test_b12(cl); + test_b12(cl); + + test_b12(std::move(cl)); + test_b12(std::move(cl)); + test_b12(std::move(cl)); + test_b12(std::move(cl)); + } + { + TestClass cl_obj(42); + TestClass *cl = &cl_obj; + test_b12(cl); + test_b12(cl); + test_b12(cl); + test_b12(cl); + } + { + DerivedFromTestClass cl_obj(42); + DerivedFromTestClass *cl = &cl_obj; + test_b12(cl); + test_b12(cl); + test_b12(cl); + test_b12(cl); + } +} + +void bullet_three_four_tests() { + { + typedef TestClass Fn; + Fn cl(42); + test_b34(cl); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + } + { + typedef DerivedFromTestClass Fn; + Fn cl(42); + test_b34(cl); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + } + { + typedef TestClass Fn; + Fn cl_obj(42); + Fn* cl = &cl_obj; + test_b34(cl); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + } + { + typedef DerivedFromTestClass Fn; + Fn cl_obj(42); + Fn* cl = &cl_obj; + test_b34(cl); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + test_b34(static_cast(cl)); + } +} + +void bullet_five_tests() { + using FooType = int&(NonCopyable&&); + { + FooType& fn = foo; + test_b5(fn); + } + { + FooType* fn = foo; + test_b5(fn); + } + { + typedef TestClass Fn; + Fn cl(42); + test_b5(cl); + test_b5(static_cast(cl)); + test_b5(static_cast(cl)); + test_b5(static_cast(cl)); + + test_b5(static_cast(cl)); + test_b5(static_cast(cl)); + test_b5(static_cast(cl)); + test_b5(static_cast(cl)); + } +} + +int main() { + bullet_one_two_tests(); + bullet_three_four_tests(); + bullet_five_tests(); +} Index: www/cxx1z_status.html =================================================================== --- www/cxx1z_status.html +++ www/cxx1z_status.html @@ -54,7 +54,7 @@ --> N3911LWGTransformationTrait Alias void_t.UrbanaComplete3.6 N4089LWGSafe conversions in unique_ptr<T[]>.Urbana - N4169LWGA proposal to add invoke function templateUrbana + N4169LWGA proposal to add invoke function templateUrbanaComplete3.6 N4190LWGRemoving auto_ptr, random_shuffle(), And Old Stuff.Urbana N4258LWGCleaning-up noexcept in the Library.UrbanaIn progress3.7 N4259CWGWording for std::uncaught_exceptionsUrbanaComplete3.7