nested private class with custom operator delete
Hi!
Compiling the code below I've got such diagnostic:
C:\App>cl cl15.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
cl15.cpp
cl15.cpp(5) : error C2248: 'A::B' : cannot access private class
declared in class 'A'
cl15.cpp(12) : see declaration of 'A::B'
cl15.cpp(9) : see declaration of 'A'
cl15.cpp(17) : see reference to function template
instantiation 'void destroy<A::B>(T *)' being compiled
with
[
T=A::B
]
The code itself:
------------------
#include <cstdlib>
template <typename T> void destroy(T* ptr)
{
delete ptr; // line #5
}
class A
{
private:
class B
{
public:
void test()
{
B* p = new B;
destroy(p);
}
~B()
{
}
void* operator new(size_t size)
{
return malloc(size);
}
void operator delete(void* p, size_t /*size*/)
{
free(p);
}
};
};
Comeau C++ web page compiles this. The real world problem was using
std::tr1::shared_ptr this way:
#include <cstdlib>
#include <memory>
class A
{
private:
class B
{
public:
void test()
{
std::tr1::shared_ptr<B> b(new B());
}
~B()
{
}
void* operator new(size_t size)
{
return malloc(size);
}
void operator delete(void* p, size_t /*size*/)
{
free(p);
}
};
};
If I remove ~B() the code compiles. If I make void destroy<B> a friend
of class B the code compiles. If I remove custom operator delete() the
code compiles.
The question is: is correct or not correct? Why should I bother about
internals of shared_ptr<> implementation to declare _Resetp<> a friend
of B in case of VS2008's Dinkumware or checked_delete<> in case of
Boost.org? This is not protable.