1

我有以下代码作为实验:

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:我试图获得返回类型Bar::f,使用result_of.

为什么它失败了,以及如何纠正它?

4

1 回答 1

2

未指定返回类型mem_fn

模板 <class R, class T>
未指定mem_fn(RT::* pm) noexcept;

根据INVOKE [func.memfn]/p1定义:

1 返回:一个简单的调用包装器([func.def]fn,使得表达式fn(t, a2, ..., aN)等价于INVOKE(pm, t, a2, ..., aN)[func.require])。

其中 的定义INVOKE包括以下两个项目符号[func.require]/p1

定义INVOKE(f, t1, t2, ..., tN)如下:

(t1.*f)(t2, ..., tN)whenf是指向类的成员函数的指针,T并且is_base_of<T, decay_t<decltype(t1)>>::valueis true

((*t1).*f)(t2, ..., tN)whenf是指向类的成员函数的指针,T不满足t1前两项;

也就是说,mem_fn返回的第一个参数必须是隐式对象参数 ( t1) 的类型,可以是引用或指针,例如:

std::result_of<decltype(std::mem_fn(&Bar::f))(Bar&)>::type y2;
//                                            ~~~^

std::result_of<decltype(std::mem_fn(&Bar::f))(Bar*)>::type y2;
//                                            ~~~^

您也可以std::mem_fn完全删除:

std::result_of<decltype(&Bar::f)(Bar*)>::type y2;
于 2016-06-22T09:17:49.240 回答