C++11 在成员函数上应用 result_of,失败,为什么?
C++11 applies result_of on member function, failed, why?
我有以下代码作为实验:
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
.
获取 return 类型的 Bar::f
为什么失败,如何纠正?
未指定return类型mem_fn
:
template <class R, class T>
unspecified mem_fn(R T::* pm) noexcept;
是根据 INVOKE
[func.memfn]/p1:
定义的
1 Returns: A simple call wrapper ([func.def]) fn
such that the expression fn(t, a2, ..., aN)
is equivalent to INVOKE(pm, t, a2, ..., aN)
([func.require]).
其中 INVOKE
的定义包括以下两个项目符号 [func.require]/p1:
Define <I>INVOKE</I>(f, t1, t2, ..., tN)
as follows:
— (t1.*f)(t2, ..., tN)
when f
is a pointer to a member function of a class T
and is_base_of<T, decay_t<decltype(t1)>>::value
is true
;
— ((*t1).*f)(t2, ..., tN)
when f
is a pointer to a member function of a class T
and t1
does not satisfy the previous two items;
即what mem_fn
return的第一个参数必须是隐式对象参数(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;
我有以下代码作为实验:
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
为什么失败,如何纠正?
未指定return类型mem_fn
:
template <class R, class T> unspecified mem_fn(R T::* pm) noexcept;
是根据 INVOKE
[func.memfn]/p1:
1 Returns: A simple call wrapper ([func.def])
fn
such that the expressionfn(t, a2, ..., aN)
is equivalent toINVOKE(pm, t, a2, ..., aN)
([func.require]).
其中 INVOKE
的定义包括以下两个项目符号 [func.require]/p1:
Define
<I>INVOKE</I>(f, t1, t2, ..., tN)
as follows:—
(t1.*f)(t2, ..., tN)
whenf
is a pointer to a member function of a classT
andis_base_of<T, decay_t<decltype(t1)>>::value
istrue
;—
((*t1).*f)(t2, ..., tN)
whenf
is a pointer to a member function of a classT
andt1
does not satisfy the previous two items;
即what mem_fn
return的第一个参数必须是隐式对象参数(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;