Home > Back-end >  About the pimpl syntax
About the pimpl syntax

Time:05-11

I have a question about the C usage used in the pimpl syntax.

First, why is it not necessary to write pimpl( new impl ) as pimpl( new my_class::impl )

Second, why is the lifetime of new impl extended even though it is a temporary object?

//my_class.h
class my_class {
   //  ... all public and protected stuff goes here ...
private:
   class impl; unique_ptr<impl> pimpl; // opaque type here
}
// my_class.cpp
class my_class::impl {  // defined privately here
  // ... all private data and functions: all of these
  //     can now change without recompiling callers ...
};
my_class::my_class(): pimpl( new impl )
{
  // ... set impl values ...
}

CodePudding user response:

First: In your constructor the expression new impl indeed is a temporary object, but it is immediately passed to the pimpl member which is not a temporary. The std::unique_ptr type stores the pointer passed to it and holds on to it until it is destroyed or moved. And the member is not destroyed until the my_class object is destroyed.

Also, consider using std::make_unique instead of new.

Second: You don't have to write my_class::impl because you are in a member function of my_class, so name resolution looks not only at namespace scope but also at class scope. So, impl can be resolved.

CodePudding user response:

First, why is it not necessary to write pimpl( new impl ) as pimpl( new my_class::impl )

Scope. When constructor is defined you are inside a cope of a class, so name lookup is able to find it without problems.

Second, why is the lifetime of new impl extended even though it is a temporary object?

You are passing temporary pointer to as constructor argument of std::unique_ptr<impl> so value is consumed by a field pimpl. impl value s not temporary since it is created on a heap, control of its lifetime was passed to smart pointer.

Extra:
You did using namespace std; in header (the only case when you can skip std:: before unique_ptr)! This is BIG mistake do not do it. This will impact dangerously all sources which will include this header. You can have symbol ambiguity error because of that.

Even having using namespace std; in cpp file is considered a bad practice.

CodePudding user response:

The new impl is not temporary because it's not a variable.

It is an expression, which

  1. is evaluated during your my_class::pimpl object member construction,
  2. its side effect is creation of an impl object (whose lifetime is controlled, not automatic, because it's not a local automatic variable),
  3. and which returns a pointer to the newly created anonymous impl object.

The expression is used as an initializer to the pimpl member of the my_class object being constructed, so the returned pointer is passed as an initializing value to pimpl and gets stored in it. This allows your my_class object to control the life time of the impl object.

The life time of the pimpl pointer variable is obviously the same as its owning my_class object's life time, however the life time of the pointed impl object depends on your my_class logic:

  1. it may keep it until its own end and get it deleted during its own destruction,
  2. it may replace() it with another one at will
  3. or release() the pimpl, delete the impl object and work without any when no longer necessary...
  4. It might even release the object and not delete it, for example to transfer the ownership to another object – or just abandon it, which causes a memory leak (and which is usually a serious error!)
  • Related