从成员函数指针获取方法的返回类型

Adr*_*thy 6 c++ member-function-pointers decltype type-traits

我试图声明一个变量,使其类型与我有成员函数指针的成员函数的返回类型相同.

class Widget {
    public:
        std::chrono::milliseconds Foo();
};
Run Code Online (Sandbox Code Playgroud)

例如,给定一个成员函数的指针fn,它指向Widget::Foo的,我怎么会声明一个变量blah,使得它得到Widget::Foo的返回类型(std::chrono::milliseconds)?

我发现从使用博客中的一些有前途的指导result_of,从<type_traits>沿decltype,但我似乎无法得到它的工作.

auto fn = &Widget::Foo;
Widget w;
std::result_of<decltype((w.*fn)())>::type blah;
Run Code Online (Sandbox Code Playgroud)

这种方法对我有意义,但VC++ 2013不喜欢它.

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xrefwrap(58): error C2064: term does not evaluate to a function taking 0 arguments
      C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xrefwrap(118) : see reference to class template instantiation 'std::_Result_of<_Fty,>' being compiled
      with
      [
          _Fty=std::chrono::milliseconds (__cdecl Widget::* )(void)
      ]
      scratch.cpp(24) : see reference to class template instantiation 'std::result_of<std::chrono::milliseconds (__cdecl Widget::* (void))(void)>' being compiled
Run Code Online (Sandbox Code Playgroud)

我不知道我做错了什么,或者这是VC++还没有处理的事情(或两者兼而有之!).我在错误消息中看到的唯一线索是__cdecl.调用约定不应该__thiscall吗?

T.C*_*.C. 6

decltype((w.*fn)()) blah;
Run Code Online (Sandbox Code Playgroud)

要么

std::result_of<decltype(fn)(Widget)>::type blah;
Run Code Online (Sandbox Code Playgroud)

  • 重要提示:“result_of”在 C++20 中已被删除。请改用“invoke_result”!https://en.cppreference.com/w/cpp/types/result_of (2认同)