2

以下情况:

class CTest
{
public:
    CTest()=default;
    ~CTest()=default;
    auto SomeFunc_Helper(std::integral_constant<int, 8> param) -> uint64_t*; //param is in reality more or less a self-implemented std::integral_constant
    auto SomeFunc() -> [how do I get the return type of SomeFunc_Helper?]
    {
        return SomeFunc_Helper(std::integral_constant<int, sizeof(uintptr_t)>{});
    }
};

因为SomeFunc()我尝试过类似的东西

auto SomeFunc() ->decltype(&CTest::SomeFunc_Helper(std::integral_constant<int, 8>))给我std::integral_constant<int, 8>无法解决的错误。所以我的问题是如何进行从一个函数到另一个函数的类型转发?(欢迎使用不包括命名空间的 C++11 解决方案std::

4

3 回答 3

3

这是宏值得的情况之一。

#define RETURNS(...) \
  noexcept(noexcept(__VA_ARGS__)) \
  -> decltype(__VA_ARGS__) \
  { return __VA_ARGS__; }

现在你可以:

auto SomeFunc()
RETURNS( SomeFunc_Helper(std::integral_constant<int, sizeof(uintptr_t)>{}) )

有人提议,RETURNS等效功能将=>在 lambdas 中变为或类似;我希望它会被普遍化。

于 2018-08-14T14:24:41.060 回答
2

你可以试试这个:

auto SomeFunc() -> decltype(SomeFunc_Helper(std::integral_constant<int, 8>()))  
{
   /* ... */
}

尾随返回类型中的参数decltype可以是任何有效的表达式,在这种情况下,是在函数体中实际执行的对成员函数的精确调用。

于 2018-08-14T13:22:17.707 回答
2

我认为您正在寻找std::result_of,但是,在这种情况下,您只需将返回类型声明为decltype(auto)(自 C++14 起)就可以了:

auto SomeFunc() -> decltype(auto)
{
    return SomeFunc_Helper(std::integral_constant<int, 8>{});
}

这样,例如,如果函数返回引用,您也可以完美地将引用转发给SomeFunction.

于 2018-08-14T13:26:15.457 回答