How is the std::tr1::shared_ptr implemented?

shared_ptr must manage a reference counter and the carrying of a deleter functor that is deduced by the type of the object given at initialization. The shared_ptr class typically hosts two members: a T* (that is returned by operator-> and dereferenced in operator*) and a aux* where aux is a inner abstract class that contains: … Read more

How does weak_ptr work?

shared_ptr uses an extra “counter” object (aka. “shared count” or “control block”) to store the reference count. (BTW: that “counter” object also stores the deleter.) Every shared_ptr and weak_ptr contains a pointer to the actual pointee, and a second pointer to the “counter” object. To implement weak_ptr, the “counter” object stores two different counters: The … Read more

Why is std::function not equality comparable?

Why is std::function not equality comparable? std::function is a wrapper for arbitrary callable types, so in order to implement equality comparison at all, you’d have to require that all callable types be equality-comparible, placing a burden on anyone implementing a function object. Even then, you’d get a narrow concept of equality, as equivalent functions would … Read more

Using generic std::function objects with member functions in one class

A non-static member function must be called with an object. That is, it always implicitly passes “this” pointer as its argument. Because your std::function signature specifies that your function doesn’t take any arguments (<void(void)>), you must bind the first (and the only) argument. std::function<void(void)> f = std::bind(&Foo::doSomething, this); If you want to bind a function … Read more