【发布时间】:2015-05-24 22:47:02
【问题描述】:
我正在尝试声明一个变量,使其类型与我有一个成员函数指针的成员函数的返回类型相同。
class Widget {
public:
std::chrono::milliseconds Foo();
};
例如,给定一个指向Widget::Foo的成员函数指针fn,
我将如何声明一个变量blah 以便它获得Widget::Foo 的返回类型(std::chrono::milliseconds)?
我从一篇博客文章中找到了一些有希望的指导,该文章使用了 <type_traits> 中的 result_of 和 decltype,但我似乎无法让它发挥作用。
auto fn = &Widget::Foo;
Widget w;
std::result_of<decltype((w.*fn)())>::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
我不知道我做错了什么,或者这是 VC++ 还没有处理的事情(或两者兼而有之!)。我在错误消息中看到的唯一线索是__cdecl。调用约定不应该是__thiscall吗?
【问题讨论】:
标签: c++ typetraits member-function-pointers decltype