Boost logo

Boost :

Subject: Re: [boost] A Pimpl variant
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-04-02 14:56:08


----- Original Message -----
From: "Stewart, Robert" <Robert.Stewart_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, April 02, 2010 7:00 PM
Subject: Re: [boost] A Pimpl variant

>
> vicente.botet wrote:
>>
>> My experience is that I add more private member functions
>> than private member data. I have explored a different variant
>> 5. Put all private funtion and static data (but not
>> instance data) into XImpl. XImpl needs only to store the back pointer.
>
> [snip]
>
>
> Wouldn't it be simpler to use static member functions?
>
> struct C::impl
> {
> static void
> ifct(C * _this)
> {
> _this->priv = ... // use private data
> _this->g(); // call public member function
> ...
> }
> };
>
> Using it from C member functions would be straightforward:
>
> void
> C::f()
> {
> impl::ifct(this);
> // compare: impl(this).ifct();
> ++impl::icount;
> }
>
> The definition of impl is simpler in my version and invoking impl member functions is one (!) character shorter. Your version requires an implicit this argument from which "that" is found; mine makes your "that" the sole argument, so its potentially faster. For those that like "this->" in front of member access, mine even looks similar (in impl's member functions).

Hi Robert,

I have considered also this possibility. One of the advantage of using an instance implementation is that we can inherit from the implementation, while using static functions doesn't allows it. Of course if a class should be able to inherit from the impl class, the class declaration must be moved to a header file.

Otherwise, you approach could be shorter, simpler and even more efficient. Note that the use of that. or this_-> is just a matter of taste and it is not realy the matter of this post.

Respect to the comaparation between

   impl::ifct(this);

and

   impl(this).ifct();

I want to add that the class C could declare a inline function This() that return impl(this) and be used as follows

   This().ifct();

which is 3 (!!!) characters shorter. But I don't think that we can compare solutions taking in account how many characters the solution use.

There is another advantage to use a This() function. We can move to a complete PImpl implementation with minor changes in the code.

Best,
Vicente


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk