Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从成员函数指针获取方法的返回类型_C++_Typetraits_Member Function Pointers_Decltype - Fatal编程技术网

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

C++ 从成员函数指针获取方法的返回类型,c++,typetraits,member-function-pointers,decltype,C++,Typetraits,Member Function Pointers,Decltype,我试图声明一个变量,以便它的类型与我有成员函数指针的成员函数的返回类型相同 class Widget { public: std::chrono::milliseconds Foo(); }; 例如,给定一个成员函数指针fn,该指针指向Widget::Foo, 我如何声明一个变量blah,以便它获得Widget::Foo的返回类型(std::chrono::millizes) 我在一篇博文中找到了一些很有希望的指导,其中使用了中的结果以及decltype,但我似乎无法让

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

class Widget {
    public:
        std::chrono::milliseconds Foo();
};
例如,给定一个成员函数指针
fn
,该指针指向
Widget::Foo
, 我如何声明一个变量
blah
,以便它获得
Widget::Foo
的返回类型(
std::chrono::millizes

我在一篇博文中找到了一些很有希望的指导,其中使用了
中的
结果以及
decltype
,但我似乎无法让它发挥作用

auto fn = &Widget::Foo;
Widget w;
std::result_of<decltype((w.*fn)())>::type blah;
auto-fn=&Widget::Foo;
小部件w;
std::result_of::type blah;
这种方法对我来说很有意义,但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
C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\include\xrefwrap(58):错误C2064:term不计算为具有0个参数的函数
C:\Program Files(x86)\Microsoft Visual Studio 12.0\VC\include\xrefwrap(118):请参阅正在编译的类模板实例化“std::\u Result\u of”的参考
具有
[
_Fty=std::chrono::毫秒(uu cdecl小部件::*)(无效)
]
cpp(24):参见正在编译的类模板实例化'std::result_'
我不知道我是做错了什么,还是VC++还不能处理(或者两者都不能处理!)。我在错误消息中看到的唯一线索是
\uu cdecl
。呼叫约定不应该是
\u thiscall

decltype((w.*fn)()) blah;

std::result_of::type blah;

不知道为什么与

std::result_of<decltype(fn)(Widget)>::type blah;
此外,我们不能创建抽象类的对象,所以如果小部件是一个抽象类,而不是它的指针在括号中,代码就不会编译

std::result_of<decltype(fn)(Widget)>::type blah = 5;
std::result_of::type blah=5;
编译错误:

error: cannot declare parameter to be of abstract type 'Widget'
  std::result_of<decltype(fn)(Widget)>::type blah = 5;//

error: incomplete type 'std::result_of<int (Widget::*(Widget))()>' used in    nested name specifier
  std::result_of<decltype(fn)(Widget)>::type blah = 5;//
错误:无法将参数声明为抽象类型“Widget”
std::result_of::type blah=5//
错误:嵌套名称说明符中使用的类型“std::result_of”不完整
std::result_of::type blah=5//
5
std::result_of<decltype(fn)(Widget)>::type blah = 5;
error: cannot declare parameter to be of abstract type 'Widget'
  std::result_of<decltype(fn)(Widget)>::type blah = 5;//

error: incomplete type 'std::result_of<int (Widget::*(Widget))()>' used in    nested name specifier
  std::result_of<decltype(fn)(Widget)>::type blah = 5;//