Question regarding usage of scoped_ptr and PIMPL

The follow little example illustrates an issue I have with scoped_ptr. I would like to use this to automatically manage destruction of a pointer to and undefined class. It seems that scope_ptr is instantiating its destructor "too early" and complaining about incomplete type for b; In my view, this should not occur as ~a() isn't defined yet. The following example fails on vc 7.1 and gcc 3.2 Note that shared_ptr works as one would hope in this context. Is there anyway to adjust scoped_ptr to work here? Robert Ramey // in my opinion this should not fail to compile under any circumstances. // however it seems that that the inline destructor of scoped_ptr // requires a complete type even though the destructor is not defined. // note: changing scoped_ptr to shared_ptr works as one would hope // note:: changing to auto_ptr still fails in with the same problem #include <boost/scoped_ptr.hpp> struct b; struct a { boost::scoped_ptr<b> pimpl; ~a(); }; int main() { a a_instance; }

Robert Ramey wrote:
The follow little example illustrates an issue I have with scoped_ptr. I would like to use this to automatically manage destruction of a pointer to and undefined class. It seems that scope_ptr is instantiating its destructor "too early" and complaining about incomplete type for b;
In my view, this should not occur as ~a() isn't defined yet.
The following example fails on vc 7.1 and gcc 3.2
You also need to declare the default constructor.
struct b;
struct a { boost::scoped_ptr<b> pimpl;
a();
~a(); };
int main() { a a_instance; }
Why, you might ask. At least I did. The implicitly generated default constructor: a(): pimpl() { } constructs pimpl and needs to instantiate ~scoped_ptr because if the body of the constructor throws, it'll need to destroy pimpl. It's pretty obvious that in our case the empty body cannot throw, but the compilers are probably right to require a definition for ~scoped_ptr.

Robert Ramey wrote:
The follow little example illustrates an issue I have with scoped_ptr. I would like to use this to automatically manage destruction of a pointer to and undefined class. It seems that scope_ptr is instantiating its destructor "too early" and complaining about incomplete type for b;
In my view, this should not occur as ~a() isn't defined yet.
The following example fails on vc 7.1 and gcc 3.2
Note that shared_ptr works as one would hope in this context. Is there anyway to adjust scoped_ptr to work here?
IMO this isn't something scoped_ptr should do. There's to much overhead associated with capturing the type at construction. I proposed something for exactly this purpose a while ago: http://lists.boost.org/MailArchives/boost/msg57577.php And I believe Peter Dimov has something similar in the files section. -- Daniel Wallin
participants (3)
-
Daniel Wallin
-
Peter Dimov
-
Robert Ramey