简体   繁体   English

使用std :: bind和成员函数,对这个参数使用对象指针与否?

[英]Using std::bind with member function, use object pointer or not for this argument?

When using std::bind to bind a member function, the first argument is the objects this pointer. 当使用std::bind绑定成员函数时,第一个参数是this指针的对象。 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: clang和GCC都没有投诉地编译它,结果适用于两个绑定:

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. 我一直试图围绕规范(第20.8.9节),但它是我不太清楚的地方之一。

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.9.1.2转发到20.8.2来描述要求和你的bind调用的效果。 20.8.2 is: 20.8.2是:

20.8.2 Requirements [func.require] 20.8.2要求[func.require]

1 Define INVOKE (f, t1, t2, ..., tN) as follows: 1定义INVOKE (f, t1, t2, ..., tN) ,如下所示:

(t1.*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is an object of type T or a reference to an object of type T or a reference to an object of a type derived from T ; - (t1.*f)(t2, ..., tN)f是指向类T的成员函数的指针时, t1是类型为T的对象或对类型为T的对象的引用或对T的引用源自T的类型的对象;

((*t1).*f)(t2, ..., tN) when f is a pointer to a member function of a class T and t1 is not one of the types described in the previous item; - ((*t1).*f)(t2, ..., tN)f是指向类T的成员函数的指针时, t1不是前一项中描述的类型之一;

t1.*f when N == 1 and f is a pointer to member data of a class T and t1 is an object of type T or a reference to an object of type T or a reference to an object of a type derived from T ; - 当N == 1时, t1.*ff是指向类T成员数据的指针, t1是类型为T的对象或对类型为T的对象的引用或对从中派生类型的对象的引用T ;

(*t1).*f when N == 1 and f is a pointer to member data of a class T and t1 is not one of the types described in the previous item; - (*t1).*fN == 1f是指向类T成员数据的指针时, t1不是前一项中描述的类型之一;

f(t1, t2, ..., tN) in all other cases. - 在所有其他情况下f(t1, t2, ..., tN)

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::shared_ptr或其他一些智能指针来保持您的实例在绑定时保持活动状态,它仍然适用于std::bind因为t1被取消引用,无论它是什么(当然,这是可能的) 。

To add to the correct answer (that both forms are allowed). 添加到正确的答案(允许两种形式)。

I think of the two binding options in analogy with function argument declaration, which may be "passed by value" or "passed by reference". 我想到了与函数参数声明类似的两个绑定选项,它可以是“通过值传递”或“通过引用传递”。

In the case of f1 (aka passing my_foo "by value") the result doesn't "see" any changes made to my_foo past the binding point. f1 (也就是通过值传递my_foo )的情况下,结果不会“看到”通过绑定点对my_foo所做的任何更改。 This may not be desired especially if my_foo evolves. 如果my_foo发展,这可能是my_foo "By value" binding has an additional "cost" of (several) calls to a copy constructor. “按值”绑定具有对复制构造函数的(几个)调用的额外“成本”。

There is a difference. 它们是有区别的。 As rytis put forward, passing by value doesn't see the changes made to my_foo. 正如rytis提出的那样,通过值传递看不到对my_foo所做的更改。 For example, in case my_foo is a class, passing by value doesn't see a change made to a member data of my_foo. 例如,如果my_foo是一个类,则传递值不会看到对my_foo的成员数据所做的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用指针成员对象的指针成员作为函数的参数 - Using a pointer member of a pointer member object as the argument of a function 在类成员中使用std :: bind和std :: function会导致回调使用旧对象引用? - Using std::bind and std::function with a class member causes the callback to use an old object reference? 如何使用std :: bind绑定带有对象指针参数的函数? - How to use std::bind to bind a function with object pointer param? std :: bind与指向函数对象的指针 - std::bind with pointer to a function object 我可以使用 std::bind 将指向成员函数的指针转换为指向函数的指针吗? - Can I use std::bind to convert a pointer to member function into a pointer to function? 在另一个函数中使用std :: bind返回对象和占位符作为参数 - Use std::bind return object and placeholder as argument in another function std :: bind参数绑定到没有对象的成员函数 - std::bind parameter to member function without object 指向std :: invoke中成员函数对象的指针 - Pointer to member function-object in std::invoke 用成员 function 将 std::bind 替换为 lambda 以填充 function 指针的向量 - Replacing std::bind with lambda with a member function to fill vector of function pointer 将带有绑定成员函数的std :: bind对象传递给函数 - Pass std::bind object with bind member function to a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM