As a heavy user of forward declarations, I enjoy my classes being complete at destruction. To ensure this, I make the destructor private and befriend boost::checked_delete
:
#include <boost/checked_delete.hpp>
struct MyClass
{
//MyClass's interface
private:
~MyClass() { /* something */ }
friend void boost::checked_delete<>(MyClass* x);
};
In C++11, std::default_delete
also checks for completeness at destruction. Nevertheless, I could not implement the same behaviour as above:
#include <memory>
struct MyClass
{
//MyClass's interface
private:
~MyClass() { /* something */ }
friend struct std::default_delete<MyClass>;
};
int main()
{
//const std::shared_ptr<MyClass> d {
// std::make_shared<MyClass>()
//}; //(1) Should compile?
const std::shared_ptr<MyClass> d(
new MyClass,std::default_delete<MyClass>()
); //(2) Does compile
}
I wonder
std::make_shared
is A Good ThingI am using GCC 4.8.0 and I checked with both -std=c++11 and -std=c++1y flags.
Something like this should work:
struct wrapper;
struct MyClass
{
private:
~MyClass() { /* something */ }
friend wrapper;
};
struct wrapper
{
MyClass obj;
};
// ...
auto const tmp = std::make_shared<wrapper>();
std::shared_ptr<MyClass> p(tmp, &tmp->obj);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With