C++ C++;11在成员函数上应用结果_失败,原因是什么?
我有以下代码作为实验:C++ C++;11在成员函数上应用结果_失败,原因是什么?,c++,c++11,types,decltype,result-of,C++,C++11,Types,Decltype,Result Of,我有以下代码作为实验: 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;//t
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!!!!!!
}
int f1(){return 0;}
结构条{
Bar()=删除;
int f(){return 0;}
int运算符(){return 1;}
};
int main()
{
decltype(f1())x=3;//f1()是表达式
结果:类型x1=3;//类型+参数
结果:类型x3=3;//类型+参数
decltype(declval().f())y=4;//表达式
decltype(((Bar*)nullptr)->*(&Bar::f))()z=5;//表达式
结果:类型y2=3;//错误!!!!!!
}
除了的最后一个结果外,一切正常:
我试图使用的result\u获取返回类型Bar::f
为什么失败,以及如何更正?未指定的返回类型mem\u fn
:
template <class R, class T>
unspecified mem_fn(R T::* pm) noexcept;
您还可以将std::mem_fn
一起删除:
std::result_of<decltype(&Bar::f)(Bar*)>::type y2;
std::result_of::type y2;
它是一个成员函数,它需要一个隐式对象参数,即:y2类型的result\u
谢谢,它可以工作,但似乎“y2类型的result\u=3;”也可以通过编译。这是正确的,为什么BAR*仍然可以被赋予这个mem_fn?使用指针还是引用类型都没有区别。这里重要的是价值类别和简历资格
std::result_of<decltype(&Bar::f)(Bar*)>::type y2;