473,387 Members | 1,673 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,387 software developers and data experts.

auto_ptr question


I saw the following post a few messages back and am trying to
understand exactly why this is so. Thanks for the information. My
comments begin with **[flopbucket]. The original post was arguing that
STL code that tried to have a container of auto_ptr (which I know is
wrong, but thats not the point here) should *NOT* compile according to
the standard.

--- start original post ---

There is a defined requirement, and if you're still in doubt, let me
spell it out. Section 23.2.4.3 of the C++ standard shows the following
function type for vector::push_back:

void push_back(const & x);

**[flopbucket] ok, I see this. But certainly you can pass a non const
object to a function that takes a const reference to that type. So I
can pass auto_ptr<void *> to something that expects const auto_ptr<void
*>&... So I don't see at this point why it should not compile.
Section 20.4.5 of the C++ standard shows the following for the auto_ptr
copy constructor:

auto_ptr(auto_ptr&) throw();
template<class Y> auto_ptr(auto_ptr<Y>&) throw();

Both copy constructors take non constant types.

**[flopbucket] Fine, makes sense. But just by calling push_back, which
takes a reference, I am not having any copy constructor being called.

So if you have a vector and an auto_ptr class that is compliant with
the C++ standard, it will not compile when you call member function
push_back.

--- end original post ---

So, my question is, why exactly does this fail to compile? I
understand that of course push_back will call the copy constructor to
copy the item for the container and at that point that template
instantiation will fail. But that is inside push_back, not the call to
push_back itself.

I guess I am just asking if my thinking is correct. I have been away
from C++ for a few years (assigned to some Java work and want to get
back into C++) and trying to catch back up with parts of the language.

So the point is that by only showing the definitions of the copy
constructors and the method signature for push_back - there is really
*NO* reason why it should not compile - it only fails because the
original poster knew details about what push_back did, correct? The
actual failure comes from the template instantiation and when push_back
tries to copy the element.

Thanks for the help

May 10 '06 #1
2 1752

flopbucket wrote:
Section 20.4.5 of the C++ standard shows the following for the auto_ptr
copy constructor:

auto_ptr(auto_ptr&) throw();
template<class Y> auto_ptr(auto_ptr<Y>&) throw();

Both copy constructors take non constant types.


Member function push_back takes type <const T&> as argument. A copy of
the original is made in push_back. The copy constructor of T is
therefore called inside push_back. The fact that T is const (from
<const T&>) means that T's copy constructor's argument has the
constraint that it must either be by value, or by const reference.
std::auto_ptr<X>'s copy constructor does not comply to this constraint.

Hope it helps.

W

May 10 '06 #2
Yes, thanks. That was what I was trying toconfirm in my confusing
original post - that the error is really because inside push_back, a
copy is made.

Thanks

May 10 '06 #3

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

Similar topics

6
by: Noah Roberts | last post by:
Often I have a method which accepts a pointer to some class as an argument. Usually that pointer is managed by the caller through std::auto_ptr. I would assume that you could pass the auto_ptr in...
2
by: Morgan Cheng | last post by:
Below code has output Foo created 3 0 doit Foo destructed 3 I am confused. When an auto_ptr object is assigned to another auto_ptr object, the control of pointer is transferred from orginal...
4
by: Rein Anders Apeland | last post by:
Consider the following working code: #include <memory> #include <iostream> void print_ptr( const std::auto_ptr< int > & thePtr = std::auto_ptr< int >() ) {
20
by: Bronek Kozicki | last post by:
Hi Please try this code. I think that it's perfectly legal. However when compiled under MSVC71 stack overflow happens in first line of main, thus second line is never executed. B.
5
by: Raider | last post by:
Is it exception-safe to write like this: void foo(auto_ptr<Tx) { ... } void bar() { foo(auto_ptr<T>(new T(...));
9
by: dragoncoder | last post by:
Hi all, I am trying to understand the auto_ptr_ref role in the implementation of auto_ptr<>. I read the information on net but still not 100% sure of it. My plan is as follows. 1. To see the...
39
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like ...
2
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called :...
2
by: dolphin | last post by:
Hi All Today , I read the source code of auto_ptr. I have a question about the copy constructor of auto_ptr. template<class _Ty> class auto_ptr { .................. } there is a copy...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.