C++ pimpl

Timon Gehr timon.gehr at gmx.ch
Sun Jan 22 17:19:35 PST 2012


On 01/23/2012 01:49 AM, Martin Nowak wrote:
> On Mon, 23 Jan 2012 01:33:47 +0100, Timon Gehr <timon.gehr at gmx.ch> wrote:
>
>> On 01/23/2012 01:07 AM, so wrote:
>>> On Mon, 23 Jan 2012 01:39:23 +0200, so <so at so.so> wrote:
>>>
>>>> I have been asking that for some time now, i am afraid you won't get
>>>> much of an audience.
>>>> You can get rid of both additional allocation and indirection but it
>>>> is not pretty. We could definitely use some help/sugar on this.
>>>>
>>>> http://www.artima.com/cppsource/backyard3.html
>>>
>>> http://www.digitalmars.com/d/archives/digitalmars/D/Implementation_hiding_139625.html
>>>
>>>
>>>
>>> There is another issue Walter forgot to mention in the article.
>>> I think there might be a way but looks like we also loose the
>>> "destructor".
>>> Which means we are all the way back to the
>>> http://en.wikipedia.org/wiki/Opaque_pointer.
>>>
>>> Walter, is there a way to get around destructor limitation?
>>
>> This seems to work.
>>
>> a.di:
>>
>> final class A{
>> private this();
>> static A factory();
>> T1 publicMember1(int x);
>> T2 publicMember2(float y);
>> T3 publicField;
>> // ...
>> }
>>
>> a.d:
>>
>> class A{
>> static A factory(){return new A;}
>> T1 publicMember1(int x){ ... }
>> T2 publicMember2(float y){ ... }
>> T3 publicField;
>> // ...
>> private:
>> T1 field1;
>> T2 field2;
>> }
>>
> This will even work with plain new/this, as the allocation is done
> in the constructor. The difficulty is that you can't build inheritable
> pimpls.

Ah, good to know. One solution would be to just add some dummy space to 
the class declaration (private void[256] dummy=void) so that the 
implementation can use it for private members. (union{void[256] dummy; 
struct{/*private_members*/}}) Of course, this is potentially wasteful.


More information about the Digitalmars-d mailing list