![](/img/trans.png)
[英]C++14 'auto' is able to get function return type, do we still need std::result_of<>?
[英]How to get the return type of a function with std::result_of?
我有一个 function 相当复杂
template<class E>
auto foo(E&& e);
我想通过使用来获取返回类型
template<class E> using Foo = decltype(foo(E{}));
这不能编译为 E&& 不好。
我尝试使用std::result_of
多种方式,但仍然失败。 有什么方法可以获取返回的类型?
编辑
foo
是
template<class E>
auto xt::strided_view(
E &&e,
const xstrided_slice_vector &slices
)
尝试了以下,不起作用
using E = xtensor<int, 2>;
using SV = xt::xstrided_slice_vector;
static_assert(is_same<
invoke_result_t<decltype(xt::strided_view<E>), E, SV>,
decltype(v)
>::value);
static_assert(is_same<
decltype(xt::strided_view(declval<E>(), sv)),
decltype(v)
>::value);
显示
error C3556: 'xt::strided_view': incorrect argument to 'decltype'
error C2955: 'std::is_same': use of class template requires template argument list
有什么方法可以获取返回的类型?
您有以下任一情况:
template<class E> using Foo = decltype(foo(std::declval<E>()));
使用std::invoke_result_t
( c++17起)
template<class E> using Foo = std::invoke_result_t<decltype(foo<E>), E>;
使用decltype
template<class E> using Foo = decltype(foo<E>(E{}));
其中,最后一个确实不推荐,因为E
应该在 function 调用中默认构造(即foo<E>(E{})
)。
我尝试使用
std::result_of
多种方式,但仍然失败。
请注意, std::result_of
在c++17中已弃用,并且(将或已经)从c++20中的标准中删除。 因此,尝试构建标准不支持的东西不是一个好主意(当您将来升级到新标准时)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.