繁体   English   中英

如何使用 std::result_of 获取 function 的返回类型?

[英]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
 )

https://xtensor.readthedocs.io/en/latest/api/xstrided_view.html#namespacext_1aca6714111810062b91a1c9e31bd69b26

尝试了以下,不起作用

    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

有什么方法可以获取返回的类型?

您有以下任一情况:

  1. 使用std::declval

     template<class E> using Foo = decltype(foo(std::declval<E>()));
  2. 使用std::invoke_result_t ( 起)

     template<class E> using Foo = std::invoke_result_t<decltype(foo<E>), E>;
  3. 使用decltype

     template<class E> using Foo = decltype(foo<E>(E{}));

其中,最后一个确实不推荐,因为E应该在 function 调用中默认构造(即foo<E>(E{}) )。


我尝试使用std::result_of多种方式,但仍然失败。

请注意, std::result_of中已弃用,并且(将或已经)从中的标准中删除。 因此,尝试构建标准不支持的东西不是一个好主意(当您将来升级到新标准时)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM