确定 return 函数类型的最简单方法
Simplest way to determine return type of function
给定一个很简单,但是很冗长的函数,比如:
int foo(int a, int b, int c, int d) {
return 1;
}
// using ReturnTypeOfFoo = ???
在编译时确定函数的return类型(ReturnTypeOfFoo
,在本例中为:int
)最简洁的方法是什么不重复函数的参数类型(仅按名称,因为已知该函数没有任何额外的重载)?
最简洁的大概是:
template <typename R, typename... Args>
R return_type_of(R(*)(Args...));
using ReturnTypeOfFoo = decltype(return_type_of(foo));
请注意,这不适用于函数对象或指向成员函数的指针。只是没有重载的函数或模板,或 noexcept
.
但如果需要,可以通过添加更多 return_type_of
.
的重载来扩展以支持所有这些情况
我不知道这是否是最简单的方法(如果您可以使用 C++17 肯定不是:请参阅 NathanOliver 的回答)但是...如何声明一个函数如下:
template <typename R, typename ... Args>
R getRetType (R(*)(Args...));
并使用 decltype()
?
using ReturnTypeOfFoo = decltype( getRetType(&foo) );
观察到 getRetType()
只是声明而没有定义,因为只调用了一个 decltype()
,所以只有返回的类型是相关的。
您可以利用 std::function
here which will give you an alias for the functions return type. This does require C++17 support, since it relies on class template argument deduction,但它适用于任何可调用类型:
using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;
我们可以让它更通用一点,比如
template<typename Callable>
using return_type_of_t =
typename decltype(std::function{std::declval<Callable>()})::result_type;
然后让您像
一样使用它
int foo(int a, int b, int c, int d) {
return 1;
}
auto bar = [](){ return 1; };
struct baz_
{
double operator()(){ return 0; }
} baz;
using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;
给定一个很简单,但是很冗长的函数,比如:
int foo(int a, int b, int c, int d) {
return 1;
}
// using ReturnTypeOfFoo = ???
在编译时确定函数的return类型(ReturnTypeOfFoo
,在本例中为:int
)最简洁的方法是什么不重复函数的参数类型(仅按名称,因为已知该函数没有任何额外的重载)?
最简洁的大概是:
template <typename R, typename... Args>
R return_type_of(R(*)(Args...));
using ReturnTypeOfFoo = decltype(return_type_of(foo));
请注意,这不适用于函数对象或指向成员函数的指针。只是没有重载的函数或模板,或 noexcept
.
但如果需要,可以通过添加更多 return_type_of
.
我不知道这是否是最简单的方法(如果您可以使用 C++17 肯定不是:请参阅 NathanOliver 的回答)但是...如何声明一个函数如下:
template <typename R, typename ... Args>
R getRetType (R(*)(Args...));
并使用 decltype()
?
using ReturnTypeOfFoo = decltype( getRetType(&foo) );
观察到 getRetType()
只是声明而没有定义,因为只调用了一个 decltype()
,所以只有返回的类型是相关的。
您可以利用 std::function
here which will give you an alias for the functions return type. This does require C++17 support, since it relies on class template argument deduction,但它适用于任何可调用类型:
using ReturnTypeOfFoo = decltype(std::function{foo})::result_type;
我们可以让它更通用一点,比如
template<typename Callable>
using return_type_of_t =
typename decltype(std::function{std::declval<Callable>()})::result_type;
然后让您像
一样使用它int foo(int a, int b, int c, int d) {
return 1;
}
auto bar = [](){ return 1; };
struct baz_
{
double operator()(){ return 0; }
} baz;
using ReturnTypeOfFoo = return_type_of_t<decltype(foo)>;
using ReturnTypeOfBar = return_type_of_t<decltype(bar)>;
using ReturnTypeOfBaz = return_type_of_t<decltype(baz)>;