【发布时间】:2016-06-21 09:33:26
【问题描述】:
我有以下代码作为实验:
int f1() { return 0; }
struct Bar {
Bar() = delete;
int f() { return 0; }
int operator()() { return 1; }
};
int main()
{
decltype(f1()) x = 3;//f1() is expression
result_of<decltype(&f1)()>::type x1 = 3;//type+param
result_of<Bar()>::type x3 = 3;//type+param
decltype(declval<Bar>().f()) y = 4;//expression
decltype((((Bar*)nullptr)->*(&Bar::f))()) z = 5;//expression
result_of<decltype(std::mem_fn(&Bar::f))()>::type y2 = 3;//error!!!!!!
}
除了最后一个result_of之外,一切正常:
我试图使用result_of 获取Bar::f 的返回类型。
为什么会失败,如何纠正?
【问题讨论】:
-
它是一个成员函数,它需要一个隐式对象参数,即
result_of<decltype(std::mem_fn(&Bar::f))(Bar)>::type y2 -
谢谢,它有效,但似乎“result_of
::type y2 = 3;”也可以通过编译。哪个是正确的,为什么 BAR* 仍然可以给这个 mem_fn? -
使用指针或引用类型没有区别。这里重要的是价值类别和简历资格
标签: c++ c++11 types decltype result-of