I'm trying to use a boost::scoped_ptr
with my implementation class that is visible only in the cpp file of the containing class. The containing class has an explicitly defined destructor (that isn't inline), but my compiler (Borland C++ 5.6.4) fails to compile.
If I use boost::shared_ptr
instead, the same example compiles and runs as expected.
What am I doing wrong?
Edit: sorry for forgetting showing the source code, compiler error, and (expected) output here it is:
Source code
File check_shared.cpp
:
// shortened.
#include "SmartPtrTest.h"
void check_shared()
{
Containing t;
}
File SmartPtrTest.h
:
#include <boost/noncopyable.hpp>
#include <boost/smart_ptr.hpp>
class Impl;
#define smart_ptr boost::scoped_ptr
class Containing: private boost::noncopyable
{
public:
Containing();
~Containing();
private:
smart_ptr<Impl> impl;
};
File SmartPtrTest.cpp
:
#include "SmartPtrTest.h"
#include <iostream>
using namespace std;
class Impl {
public:
Impl() {
cout << "ctr Impl" << endl;
}
~Impl() {
cout << "dtr Impl" << endl;
}
};
Containing::Containing(): impl(new Impl)
{
cout << "ctr Containing" << endl;
}
Containing::~Containing()
{
cout << "dtr Containing" << endl;
}
The compiler error
...is something like undefined structure 'Impl'
(it's German: Undefinierte Struktur 'Impl'). When compiling the file check_shared.cpp
the compiler stops in the file boost/checked_delete.hpp
in the typedef
of this function:
template<class T> inline void checked_delete(T * x)
{
// intentionally complex - simplification causes regressions
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
(void) sizeof(type_must_be_complete);
delete x;
}
The output (expected)
This output I'm getting when using boost::share_ptr
, showing that ctr and dtr are called as expected.
ctr Impl
ctr Containing
dtr Containing
dtr Impl