|
Boost : |
From: Beman Dawes (bdawes_at_[hidden])
Date: 2001-04-03 16:54:02
>> >// C.hpp
>> >class Cimpl;
>> >class C {
>> >public:
>> > ~C();
>> >private:
>> > boost::scoped_ptr<Cimpl> impl;
>> >};
>> >
>> >// C.cpp
>> >class Cimpl {
>> >};
>> >
>> >C::~C()
>> >{
>> >}
>> >
>> >Am I missing something, or are you talking about a different idiom?
>>
>> That is the idiom I was talking about.
At 03:03 PM 4/3/2001, David Abrahams wrote:
>With the code as written, the definition of Cimpl is only needed in
C.cpp,
>which nicely shields the Pimpl's users from implementation details. Since
>scoped_ptr<CImpl>::~scoped_ptr() is only instantiated in c.cpp, there
>shouldn't be any problem with mentioning sizeof(T) (i.e. sizeof(Cimpl))
>there.
At 03:13 PM 4/3/2001, Rainer Deyke wrote:
>Then there shouldn't be a problem. Declaring '~C' in 'C' and placing the
>definition for 'C::~C' below the definition for 'Cimpl' causes 'Cimpl' to
>be a complete type at the point where 'scoped_ptr<Cimpl>::~scoped_ptr' is
>called. 'Cimpl' doesn't even need a trivial destructor.
Ah! The light dawns on Marblehead! I had been misreading C::~C as
Cimpl::~Cimpl.
This is starting to seem more familiar. Isn't it a variation of something
Kevin Atkinson did in his clone_ptr? (Apologies if I've misremembered his
name.)
It would apply to other smart pointers, too, wouldn't it? shared_ptr,
auto_ptr, etc?
The downside is that it will break some existing code, although the fix is
trivial. But it does increase safety, so is worth doing. Where does that
leave std::auto_ptr<>? Odd man out, as usual, I guess.
--Beman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk