// Copyright 2018 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "google/cloud/internal/invoke_result.h" #include namespace google { namespace cloud { inline namespace GOOGLE_CLOUD_CPP_NS { namespace internal { namespace { TEST(ResultOfTest, Lambda) { // This test mostly uses static assertions because the result_of classes are // largely meta-functions. auto l = [](long, int) -> int { return 7; }; // NOLINT(google-runtime-int) using F = decltype(l); using R = invoke_result::type; // NOLINT(google-runtime-int) static_assert(std::is_same::value, "expected `int` in invoke_result_t<>"); } std::string test_function(int, std::string const&) { return "42"; } TEST(ResultOfTest, Function) { // This test mostly uses static assertions because the result_of classes are // largely meta-functions. using R1 = decltype(invoker_function(test_function, 7, std::string{})); static_assert(std::is_same::value, "expected `std::string` in R1==decltype(invoker_function())"); using R2 = decltype(invoker_function(test_function, std::declval(), std::declval())); static_assert(std::is_same::value, "expected `std::string` in R2==decltype(invoker_function())"); EXPECT_TRUE((std::is_same::value)); using F = decltype(test_function); using R3 = decltype(invoker_function(std::declval(), std::declval(), std::declval())); static_assert(std::is_same::value, "expected `std::string` in R3==decltype(invoker_function())"); EXPECT_TRUE((std::is_same::value)); using R4 = invoke_result; static_assert(std::is_same::value, "expected `std::string` in R4==invoke_result_t<>"); static_assert( is_invocable::value, "expected `is_invocable` to be true"); static_assert(is_invocable::value, "expected `is_invocable` to be true"); static_assert(is_invocable::value, "expected `is_invocable` to be true"); static_assert(!is_invocable::value, "expected `is_invocable` to be false"); static_assert(!is_invocable::value, "expected `is_invocable` to be false"); // Some compilers create warnings/errors if the function is not used, even // though in this test we are mostly interested in its type. EXPECT_EQ("42", test_function(7, "7")); } struct TestStruct { void DoSomething(std::string const&, int) {} template void DoSomethingTemplated(std::string const&, F&&) {} }; TEST(ResultOfTest, TestMemberFn) { using DoSomethingType = decltype(&TestStruct::DoSomething); static_assert(is_invocable::value, "expected `is_invocable` to be true"); using DoSomethingTemplatedType = decltype(&TestStruct::DoSomethingTemplated); static_assert(is_invocable::value, "expected `is_invocable` to be true"); using DoSomethingType = decltype(&TestStruct::DoSomething); static_assert(!is_invocable::value, "expected `is_invocable` to be true"); using DoSomethingTemplatedType = decltype(&TestStruct::DoSomethingTemplated); static_assert(!is_invocable::value, "expected `is_invocable` to be true"); } } // namespace } // namespace internal } // namespace GOOGLE_CLOUD_CPP_NS } // namespace cloud } // namespace google