diff --git a/SG14/inplace_function.h b/SG14/inplace_function.h index 59453966..facce9e8 100644 --- a/SG14/inplace_function.h +++ b/SG14/inplace_function.h @@ -105,9 +105,11 @@ template struct vtable template explicit constexpr vtable(wrapper) noexcept : invoke_ptr{ [](storage_ptr_t storage_ptr, Args&&... args) -> R - { return (*static_cast(storage_ptr))( - static_cast(args)... - ); } + { + return static_cast((*static_cast(storage_ptr))( + static_cast(args)... + )); + } }, copy_ptr{ [](storage_ptr_t dst_ptr, storage_ptr_t src_ptr) -> void { ::new (dst_ptr) C{ (*static_cast(src_ptr)) }; } @@ -151,20 +153,31 @@ struct is_valid_inplace_dst : std::true_type }; // C++11 MSVC compatible implementation of std::is_invocable_r. +// By default, our is_invocable_r is FALSE, +// even though std::is_invocable_r is TRUE. +// Set SG14_INPLACE_FUNCTION_INVOCABLE_AS_VOID if you want that behavior. template void accept(R); template struct is_invocable_r_impl : std::false_type {}; template struct is_invocable_r_impl< +#if SG14_INPLACE_FUNCTION_INVOCABLE_AS_VOID decltype(std::declval()(std::declval()...), void()), +#else + decltype(std::declval()(std::declval()...)), +#endif void, F, Args... > : std::true_type {}; template struct is_invocable_r_impl< +#if SG14_INPLACE_FUNCTION_INVOCABLE_AS_VOID decltype(std::declval()(std::declval()...), void()), +#else + decltype(std::declval()(std::declval()...)), +#endif const void, F, Args... diff --git a/SG14_test/inplace_function_test.cpp b/SG14_test/inplace_function_test.cpp index 6aaffff3..52702cec 100644 --- a/SG14_test/inplace_function_test.cpp +++ b/SG14_test/inplace_function_test.cpp @@ -476,6 +476,7 @@ static void test_convertibility_with_lambdas() const auto a = []() -> int { return 3; }; static_assert(std::is_convertible>::value, ""); + static_assert(std::is_convertible>::value, ""); static_assert(!std::is_convertible>::value, ""); static_assert(!std::is_convertible>::value, ""); @@ -485,7 +486,13 @@ static void test_convertibility_with_lambdas() static_assert(!std::is_convertible>::value, ""); const auto c = [](int, NoDefaultCtor) -> int { return 3; }; + static_assert(std::is_convertible>::value, ""); + static_assert(std::is_convertible>::value, ""); +#if SG14_INPLACE_FUNCTION_INVOCABLE_AS_VOID static_assert(std::is_convertible>::value, ""); +#else + static_assert(!std::is_convertible>::value, ""); +#endif static_assert(!std::is_convertible>::value, ""); static_assert(!std::is_convertible>::value, ""); @@ -500,16 +507,32 @@ static void test_convertibility_with_lambdas() // Same as a, but not const. auto e = []() -> int { return 3; }; static_assert(std::is_convertible>::value, ""); + static_assert(std::is_convertible>::value, ""); static_assert(!std::is_convertible>::value, ""); static_assert(!std::is_convertible>::value, ""); // Same as a, but not const and mutable. auto f = []() mutable -> int { return 3; }; static_assert(std::is_convertible>::value, ""); + static_assert(std::is_convertible>::value, ""); static_assert(!std::is_convertible>::value, ""); static_assert(!std::is_convertible>::value, ""); } +static void test_void_returning_function_runtime() +{ + auto lambda = []() { return 42; }; +#if SG14_INPLACE_FUNCTION_INVOCABLE_AS_VOID + static_assert(std::is_convertible>::value, ""); + + stdext::inplace_function f = lambda; + f = lambda; + f(); +#else + static_assert(!std::is_convertible>::value, ""); +#endif +} + namespace { struct InstrumentedCopyConstructor { static int copies; @@ -568,11 +591,22 @@ static void test_is_invocable() static_assert(! is_invocable_r::value, ""); static_assert(! is_invocable_r::value, ""); + static_assert(is_invocable_r::value, ""); + static_assert(! is_invocable_r::value, ""); + static_assert(! is_invocable_r::value, ""); + static_assert(is_invocable_r::value, ""); static_assert(! is_invocable_r::value, ""); + static_assert(is_invocable_r::value, ""); + static_assert(! is_invocable_r::value, ""); + // Testing widening and narrowing conversions, and the "conversion" to void. +#if SG14_INPLACE_FUNCTION_INVOCABLE_AS_VOID static_assert(is_invocable_r::value, ""); +#else + static_assert(! is_invocable_r::value, ""); +#endif static_assert(is_invocable_r::value, ""); static_assert(is_invocable_r::value, ""); @@ -583,9 +617,14 @@ static void test_is_invocable() // > Determines whether Fn can be invoked with the arguments ArgTypes... // > to yield a result that is convertible to R. // - // void is treated specially because a functions return value can be ignored. + // void is treated specially because a function's return value can be ignored. +#if SG14_INPLACE_FUNCTION_INVOCABLE_AS_VOID static_assert(is_invocable_r::value, ""); static_assert(is_invocable_r::value, ""); +#else + static_assert(! is_invocable_r::value, ""); + static_assert(! is_invocable_r::value, ""); +#endif // Regression tests for both is_invocable and is_convertible. static_assert(is_invocable_r::value, ""); @@ -616,6 +655,17 @@ static void test_overloading_on_return_type() EXPECT_EQ(overloaded_function3([](int) { return nullptr; }), 2); } +#if !(SG14_INPLACE_FUNCTION_INVOCABLE_AS_VOID) +static int overloaded_function4(stdext::inplace_function) { return 1; } +static int overloaded_function4(stdext::inplace_function) { return 2; } + +static void test_overloading_on_return_type_void() +{ + EXPECT_EQ(overloaded_function4([]() -> void {}), 1); + EXPECT_EQ(overloaded_function4([]() -> int { return 0; }), 2); +} +#endif + void sg14_test::inplace_function_test() { // first set of tests (from Optiver) @@ -703,11 +753,15 @@ void sg14_test::inplace_function_test() test_is_convertible(); test_convertibility_with_qualified_call_operators(); test_convertibility_with_lambdas(); + test_void_returning_function_runtime(); test_return_by_move(); test_is_invocable(); test_overloading_on_arity(); test_overloading_on_parameter_type(); test_overloading_on_return_type(); +#if !(SG14_INPLACE_FUNCTION_INVOCABLE_AS_VOID) + test_overloading_on_return_type_void(); +#endif } #ifdef TEST_MAIN