Skip to content

Commit

Permalink
Extract FunctionReturnType to catch_meta.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
horenmar committed Oct 28, 2019
1 parent 5290d4b commit fc320f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
14 changes: 2 additions & 12 deletions include/internal/catch_generators_generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define TWOBLUECUBES_CATCH_GENERATORS_GENERIC_HPP_INCLUDED

#include "catch_generators.hpp"
#include "catch_meta.hpp"

namespace Catch {
namespace Generators {
Expand Down Expand Up @@ -172,18 +173,7 @@ namespace Generators {
}
};

#if defined(__cpp_lib_is_invocable) && __cpp_lib_is_invocable >= 201703
// std::result_of is deprecated in C++17 and removed in C++20. Hence, it is
// replaced with std::invoke_result here. Also *_t format is preferred over
// typename *::type format.
template <typename Func, typename U>
using MapFunctionReturnType = std::remove_reference_t<std::remove_cv_t<std::invoke_result_t<Func, U>>>;
#else
template <typename Func, typename U>
using MapFunctionReturnType = typename std::remove_reference<typename std::remove_cv<typename std::result_of<Func(U)>::type>::type>::type;
#endif

template <typename Func, typename U, typename T = MapFunctionReturnType<Func, U>>
template <typename Func, typename U, typename T = FunctionReturnType<Func, U>>
GeneratorWrapper<T> map(Func&& function, GeneratorWrapper<U>&& generator) {
return GeneratorWrapper<T>(
pf::make_unique<MapGenerator<T, U, Func>>(std::forward<Func>(function), std::move(generator))
Expand Down
38 changes: 25 additions & 13 deletions include/internal/catch_meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,34 @@
#include <type_traits>

namespace Catch {
template<typename T>
struct always_false : std::false_type {};
template<typename T>
struct always_false : std::false_type {};

template <typename> struct true_given : std::true_type {};
struct is_callable_tester {
template <typename Fun, typename... Args>
true_given<decltype(std::declval<Fun>()(std::declval<Args>()...))> static test(int);
template <typename...>
std::false_type static test(...);
};
template <typename> struct true_given : std::true_type {};
struct is_callable_tester {
template <typename Fun, typename... Args>
true_given<decltype(std::declval<Fun>()(std::declval<Args>()...))> static test(int);
template <typename...>
std::false_type static test(...);
};

template <typename T>
struct is_callable;
template <typename T>
struct is_callable;

template <typename Fun, typename... Args>
struct is_callable<Fun(Args...)> : decltype(is_callable_tester::test<Fun, Args...>(0)) {};
template <typename Fun, typename... Args>
struct is_callable<Fun(Args...)> : decltype(is_callable_tester::test<Fun, Args...>(0)) {};


#if defined(__cpp_lib_is_invocable) && __cpp_lib_is_invocable >= 201703
// std::result_of is deprecated in C++17 and removed in C++20. Hence, it is
// replaced with std::invoke_result here. Also *_t format is preferred over
// typename *::type format.
template <typename Func, typename U>
using FunctionReturnType = std::remove_reference_t<std::remove_cv_t<std::invoke_result_t<Func, U>>>;
#else
template <typename Func, typename U>
using FunctionReturnType = typename std::remove_reference<typename std::remove_cv<typename std::result_of<Func(U)>::type>::type>::type;
#endif

} // namespace Catch

Expand Down

0 comments on commit fc320f6

Please sign in to comment.