result_of作用与类的成员函数,编译失败了,怎么改?
我做了一个小的实验:#include<functional>
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!!!!!!
return 0;
}
我知道decltype跟一个表达式,result_of跟一个类型做模板参数,没有问题,那么我想得到Bar这个类里面函数f的返回类型,用result_of该怎么写呢? std::mem_fn(&Bar::f) 这个函数调用时是要传入一个Bar参数的呀
改为 result_of<decltype(std::mem_fn(&Bar::f))(Bar)>::type 试试
其实用 decltype( declval<Bar>().f() ) 更好一些吧 本帖最后由 cdsfiui 于 2016-06-22 09:59 编辑
bruceteen 发表于 2016-06-22 09:03 static/image/common/back.gif
std::mem_fn(&Bar::f) 这个函数调用时是要传入一个Bar参数的呀
改为 result_of::type 试试
谢谢,不过有个问题:
result_of<decltype(std::mem_fn(&Bar::f))(Bar*)>::type y2 = 3;
result_of<decltype(std::mem_fn(&Bar::f))(Bar)>::type y2 = 3;
上面这两个都能通过编译。但是应该只有一个版本是正确的吧?
还请指教! 回复 3# cdsfiui
一样吧,mem_fn能辨别出你传入的是对象还是指针。
页:
[1]