Boost logo

Boost :

From: Martin Bonner (martin.bonner_at_[hidden])
Date: 2005-09-27 10:28:41


----Original Message----
From: Asger Mangaard [mailto:tmb_at_[hidden]]
Sent: 27 September 2005 16:13
To: boost_at_[hidden]
Subject: Re: [boost] Are there any interest in a pimpl library?

>> Could you be more specific as to what functionality such a library
>> would supply? If you already have an implementation then an example
>> of it's use would be interesting.

That was my immediate reaction too.

>> It is my understanding that the pimpl idiom is fairly simple yet
>> also class specific. I currently implement it like this:
[snip classic pimpl idiom]

> The only thing my library does is to initialize the forward declared
> class, using heap or static memory through policies. Also, provides an
> easy way of accessing the data.
>
> Eg.
>
> .hpp
>
> boost::pimpl<struct CMyData> m_MyData;
>
> .cpp
>
> struct CMyData
> {
> CMyData()
> : m_Integer(0)
> {
> }
> int m_Integer;
> };
>
> /// Data access
> m_MyData->m_Integer = 2341;
>
> The CMyData will be initialized the first time its data members are
> accessed.

That means each use of m_MyData operator ->() has got to check whether the
structure is initialized or not, and initialize it if so. Isn't it much
easier to initialize the pimpl struct in the constructor of the outer class?

I also don't see how much easier it can get to access the data than to
write:

  pimpl->m_Integer

-- 
Martin Bonner
Martin.Bonner_at_[hidden]
Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ,
ENGLAND Tel: +44 (0)1223 441434

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