473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

AutoPointer template


To understand auto_ptr I mocked up this replacement class. This is also
basically my first usage of templates.

template<class X> class AutoPointer {

public:

AutoPointer(X* clazz)
: mClass(clazz)
{
}

~AutoPointer()
{
delete mClass;
}

X& operator*() const
{
return *mClass;
}

X* operator->() const
{
return mClass;
}

private:

X* mClass;

};

Now, I have this:

class Foo {

public:

Foo()
{
::printf("Foo constructor\n");
}

~Foo()
{
::printf("Foo destructor\n");
}

};

int main()
{
AutoPointer<Foo> foo = new Foo;
AutoPointer<char> bar = new char[10];
}

And that outputs the expected. Great. Heap objects that are automatically
destroyed when they go out of scope. Didn't know it would be that easy.

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? So why does it accept a char* in this case? Is there
any way to prevent that?

S.
Jul 22 '05 #1
4 2973
Stefan Arentz wrote:
To understand auto_ptr I mocked up this replacement class. This is also
basically my first usage of templates.

template<class X> class AutoPointer {

public:

AutoPointer(X* clazz)
: mClass(clazz)
{
}

~AutoPointer()
{
delete mClass;
}

X& operator*() const
{
return *mClass;
}

X* operator->() const
{
return mClass;
}

private:

X* mClass;

};

Now, I have this:

class Foo {

public:

Foo()
{
::printf("Foo constructor\n");
}

~Foo()
{
::printf("Foo destructor\n");
}

};

int main()
{
AutoPointer<Foo> foo = new Foo;
AutoPointer<char> bar = new char[10];
}

And that outputs the expected. Great. Heap objects that are automatically
destroyed when they go out of scope. Didn't know it would be that easy.
Actually, you're invoking undefined behaviour. The 'bar' object frees
its 'mClass' variable using 'delete' while the pointer it got was obtained
using 'new[]'. That's a big mistake.

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.

I don't mean to rain on your parade, but your AutoPointer class is
also defective in a different way: it does not have proper copying set up
(copy c-tor and the assignment op).

Victor
Jul 22 '05 #2
Victor Bazarov <v.********@comAcast.net> writes:

....
Actually, you're invoking undefined behaviour. The 'bar' object frees
its 'mClass' variable using 'delete' while the pointer it got was obtained
using 'new[]'. That's a big mistake.


Well that is why I ask the real question a little bit below in this posting.
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 :)

S.
Jul 22 '05 #3

"Stefan Arentz" <st***********@gmail.com> wrote in message
news:87************@keizer.soze.com...

To understand auto_ptr I mocked up this replacement class. This is also
basically my first usage of templates.
Bad choice for your first use of templates. std::auto_ptr may actually be
impossible to implement correctly as currently specified. (See some of the
references cited in the link below.)
int main()
{
AutoPointer<Foo> foo = new Foo;
AutoPointer<char> bar = new char[10];
}


If you want arrays to be treated correcty, try this implementation of move_ptr:

http://home.comcast.net/~jturkanis/move_ptr/

(Disclaimer: it's not currently part of Boost)

Usage is as follows:

int main()
{
boost::static_move_ptr<Foo> foo(new Foo);
boost::static_move_ptr<char[]> bar(new char[10]);
}

(This syntax was suggested by Howard Hinnant.)

Best Regards,
Jonathan
Jul 22 '05 #4
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.