我正在研究C ++ 11的 enable_shared_from_this ;一个例子让我感到困惑: shared_from_this()返回的 shared_ptr 类型如何转换为该原始指针?
I'm studying enable_shared_from_this of C++11 now; one example made me confused: how the shared_ptr type returned by shared_from_this() can convert to this raw pointer?
#include <iostream> #include <memory> #include <functional> struct Bar { Bar(int a) : a(a) {} int a; }; struct Foo : public std::enable_shared_from_this<Foo> { Foo() { std::cout << "Foo::Foo\n"; } ~Foo() { std::cout << "Foo::~Foo\n"; } std::shared_ptr<Bar> getBar(int a) { std::shared_ptr<Bar> pb( new Bar{a}, std::bind(&Foo::showInfo, shared_from_this(), std::placeholders::_1) ); return pb; } void showInfo(Bar *pb) { std::cout << "Foo::showInfo()\n"; delete pb; } }; int main() { std::shared_ptr<Foo> pf(new Foo); std::shared_ptr<Bar> pb = pf->getBar(10); std::cout << "pf use_count: " << pf.use_count() << std::endl; }推荐答案
这是 std :: bind 被
如Callable中所述,当调用指向非静态成员函数的指针或指向非静态数据的指针时成员,第一个参数必须是对其成员将被访问的对象的引用或指针(可能包括智能指针,如std :: shared_ptr和std :: unique_ptr)。
As described in Callable, when invoking a pointer to non-static member function or pointer to non-static data member, the first argument has to be a reference or pointer (including, possibly, smart pointer such as std::shared_ptr and std::unique_ptr) to an object whose member will be accessed.
bind 已实现,因此它可以接受智能指针代替原始指针。
bind is implemented so it can accept a smart pointer in lieu of a raw pointer.
您可以在 glibc ++实现比 bind 内部调用 invoke :
You can see in the glibc++ implementation than bind internally calls invoke:
// Call unqualified template<typename _Result, typename... _Args, std::size_t... _Indexes> _Result __call(tuple<_Args...>&& __args, _Index_tuple<_Indexes...>) { return std::__invoke(_M_f, _Mu<_Bound_args>()(std::get<_Indexes>(_M_bound_args), __args)... ); }和 std :: invoke 可用于智能事物(指针,引用包装器等),开箱即用:
INVOKE(f,t1,t2,...,tN)的定义如下:
如果 f 是指向类 T 的成员函数的指针:
If f is a pointer to member function of class T:
- 如果 std :: is_base_of< T,则std :: decay_t< decltype(t1)> :: value 是如果为true,则 INVOKE(f,t1,t2,...,tN)等效于(t1。* f)(t2,.. 。,tN)
- 如果 std :: decay_t< decltype(t1)> 是专业化 std :: reference_wrapper 的值,则 INVOKE(f,t1,t2,...,tN)等效于(t1.get()。* f)(t2,...,tN)
- 如果 t1 确实不满足前面的项目,则 INVOKE(f,t1,t2,...,tN)等效于((* t1)。* f)(t2,...,tN) 。
- If std::is_base_of<T, std::decay_t<decltype(t1)>>::value is true, then INVOKE(f, t1, t2, ..., tN) is equivalent to (t1.*f)(t2, ..., tN)
- If std::decay_t<decltype(t1)> is a specialization of std::reference_wrapper, then INVOKE(f, t1, t2, ..., tN) is equivalent to (t1.get().*f)(t2, ..., tN)
- If t1 does not satisfy the previous items, then INVOKE(f, t1, t2, ..., tN) is equivalent to ((*t1).*f)(t2, ..., tN).
更多推荐
这个shared
发布评论