假设我们有一个函数f
,它返回一些未知类型的值(让我们称之为T
)并将T
类型的值作为参数(可能还有其他一些论点)。如何在C ++ 14中获得f
的返回类型?
如果我们知道参数类型(通过std::result_of
),就有办法做到这一点。如果我们知道除T
之外的所有参数类型都可以吗?
示例:
template <class F> // F is functor with T operator()(T a, T b)
class A {
// Here I want to do
// T some_function(T some_arg) { ... }
}
答案 0 :(得分:10)
template <typename T>
struct return_type;
template <typename R, typename... Args>
struct return_type<R(Args...)> { using type = R; };
template <typename R, typename... Args>
struct return_type<R(*)(Args...)> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...)> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) &> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) &&> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const&> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const&&> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) volatile> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) volatile&> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) volatile&&> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const volatile> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const volatile&> { using type = R; };
template <typename R, typename C, typename... Args>
struct return_type<R(C::*)(Args...) const volatile&&> { using type = R; };
template <typename T>
using return_type_t = typename return_type<T>::type;
测试:
#include <type_traits>
struct Functor
{
int operator()(int i, int j) { return i + j; }
};
template <class F>
struct A
{
using T = return_type_t<decltype(&F::operator())>;
T some_function(T some_arg) { return some_arg; }
};
int main()
{
A<Functor> a;
static_assert(std::is_same<decltype(a.some_function(1)), int>::value, "!");
}
答案 1 :(得分:5)
建立在@Piotr S。对于任意仿函数类型的优秀答案,如果你知道正确的一个operator()
重载与正确的&#34;模式&#34; ,可以这么说,你可以这样做:
// leave undefined
template<class C, class T>
T return_type_helper(T (C::*)(T));
template<class C, class T>
T return_type_helper(T (C::*)(T) const);
// 10 additional combinations of ref- and cv- qualifiers omitted, because I'm lazy
template<typename T>
using functor_return_type = decltype(return_type_helper(&T::operator()));
这会激活重载决策和模板参数推断,以确定正确的operator()
。
然后你可以将这两者结合起来:
template <typename... T>
struct voider { using type = void; };
template <typename... T>
using void_t = typename voider<T...>::type;
template<typename T, typename = void>
struct combined_return_type;
template<typename T>
struct combined_return_type<T, void_t<typename return_type<T>::type>> {
using type = typename return_type<T>::type;
};
template<typename T>
struct combined_return_type<T, void_t<functor_return_type<T>>> {
using type = functor_return_type<T>;
};
template <typename T>
using return_type_t = typename combined_return_type<T>::type;