使用std :: bind与成员函数,使用对象指针或不是这个参数? [英] Using std::bind with member function, use object pointer or not for this argument?
问题描述
当使用 std :: bind
绑定成员函数时,第一个参数是对象 this
指针。
请参阅以下程序:
#include< iostream>
#include< functional>
struct foo
{
void bar(int v){std :: cout< foo :: bar - < v<< '\\\
'; }
};
int main()
{
foo my_foo;
auto f1 = std :: bind(& foo :: bar,my_foo,1);
auto f2 = std :: bind(& foo :: bar,& my_foo,2);
f1();
f2();
}
两个clang和GCC编译这个没有投诉, :
foo :: bar - 1
foo :: bar - 2
我一直在试图围绕规范(第20.8.9节),但它是一个非常不清楚的地方。
应该只有一个是正确的,还是正确的?
20.8.9.1.2转到20.8.2来描述您对 bind
的调用的要求和效果。 20.8.2是:
20.8.2要求[func.require]
1 定义 INVOKE
(f,t1,t2,...,tN)
如下:
-
(t1。* f)(t2,...,tN)
/ code>是指向类T
和t1
的成员函数的指针是类型T
或对类型T
的对象的引用,或对从T
;
-
((* t1)。* f)当
不是上一个项目中描述的类型之一;f
是指向类T
的成员函数的指针时, code> t1
-
派生类型的对象的引用当t1。当
N == 1
和f
是指向类的成员数据的指针时,T
和t1
是类型T
引用T
类型的对象或对从T
;N == 1
时,
-
(* t1)。* f
和f
是指向类T
和t1 $ c的成员数据的指针$ c>不是上一个项目中描述的类型之一;
-
f(t1,t2,...,tN) / code>在所有其他情况下。
前两个选项允许引用和指针。
这里需要注意的重要的事情是,不是限制你使用简单的指针。你可以使用一个 std :: shared_ptr
或其他一些智能指针来保持你的实例活着,而绑定它仍然可以使用 std :: bind
作为 t1
被取消引用,无论是什么(给定,当然,这是可能的)。
When using std::bind
to bind a member function, the first argument is the objects this
pointer. However it works passing the object both as a pointer and not.
See for example the following program:
#include <iostream>
#include <functional>
struct foo
{
void bar(int v) { std::cout << "foo::bar - " << v << '\n'; }
};
int main()
{
foo my_foo;
auto f1 = std::bind(&foo::bar, my_foo, 1);
auto f2 = std::bind(&foo::bar, &my_foo, 2);
f1();
f2();
}
Both clang and GCC compiles this without complaints, and the result works for both binds:
foo::bar - 1 foo::bar - 2
I have been trying to wrap my head around the specification (section 20.8.9) but it's one of the places where it's far from clear to me.
Should only one be correct, or are both correct?
Both are correct. 20.8.9.1.2 forwards to 20.8.2 to describe the requirements and the effect of your call to bind
. 20.8.2 is:
20.8.2 Requirements [func.require]
1 Define INVOKE
(f, t1, t2, ..., tN)
as follows:—
(t1.*f)(t2, ..., tN)
whenf
is a pointer to a member function of a classT
andt1
is an object of typeT
or a reference to an object of typeT
or a reference to an object of a type derived fromT
;—
((*t1).*f)(t2, ..., tN)
whenf
is a pointer to a member function of a classT
andt1
is not one of the types described in the previous item;—
t1.*f
whenN == 1
andf
is a pointer to member data of a classT
andt1
is an object of typeT
or a reference to an object of typeT
or a reference to an object of a type derived fromT
;—
(*t1).*f
whenN == 1
andf
is a pointer to member data of a classT
andt1
is not one of the types described in the previous item;—
f(t1, t2, ..., tN)
in all other cases.
The first two options allow both a reference and a pointer.
The important thing to notice here is that the wording does not limit you to plain pointers. You could use a std::shared_ptr
or some other smart pointer to keep your instance alive while bound and it would still work with std::bind
as t1
is dereferenced, no matter what it is (given, of course, that it's possible).
这篇关于使用std :: bind与成员函数,使用对象指针或不是这个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!