Stefan Arentz wrote:
But, in the 'the finally debate' thread somebody gave this auto_ptr
example:
std::auto_ptr<char> my_ptr(new char[987]);
Which translates to this with my template:
AutoPointer<char> bar = new char[10];
It compiles fine, which I find odd, since the template definition
states:
template<class X> class AutoPointer {
^^^^^
that X should be a class right?
No, it can be of any type for which certain requested operations are
provided (like declaring a pointer of).
> So why does it accept a char* in this case? Is there
any way to prevent that?
No, there is no way to prevent that.
Bah I hope you are wrong. Or else this is another great way to shoot++ yourself
in the foot :)
A pointer is just a pointer whether it points to a single object or to
an array of them. There is no platform-independent way to figure out
the difference. The language states that anything allocated using
'new[]' has to be disposed of using 'delete[]' and, symmetrically,
anything allocated using 'new' needs 'delete' to be freed. That is
to some extend a limitation of the language, and I've heard scores of
people complaining about it. But it's not going to change any time
soon. Or maybe it will. Ask in comp.std.c++, they talk about the
Standard document and its upcoming changes.
As to prohibiting your AutoPointer from being used with 'char', that's
relatively easy: define a specialisation of AutoPointer, and define all
its members private. There would be no way to instantiate it.
Victor