473,748 Members | 9,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Learning auto_ptr_ref role in auto_ptr implementation

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 behaviour of std::auto_ptr.
2. To implement a pkt::auto_ptr without auto_ptr_ref.
3. Check out the limitations of pkt::auto_ptr as compared to
std::auto_ptr (using steps 1 and 2).
4. Try to relate the concept of auto_ptr_ref to fill the gaps found in
step 3.

NOTE: Any siggestions on this are welcome.

As the first step I have written the following program.

=cat example.cxx
#include <iostream>
#include <memory>

using namespace std;

template <typename T>
auto_ptr <Tsource ( T a )
{
return auto_ptr <T( new T ( a ) );
}

template <typename T>
void print ( /* const */ auto_ptr <T>& p )
{
cout << "In print(): " << *p << endl;
}

int main()
{
print ( source ( 5 ) );
return 0;
}

z852378@premier :/home/z852378/rnd/pkt
=g++ example.cxx
example.cxx: In function `int main()':
example.cxx:20: could not convert `source(T) [with T = int]()' to `
std::auto_ptr<i nt>&'
example.cxx:14: in passing argument 1 of `void print(std::auto _ptr<T>&)
[with T
= int]'

Uncommenting the 'const' in print() definition compiles the code fine
and the output is also fine. Based on my current knowledge, this is
because the return value of source() is a temporary and therefore can
not be bound to a non-const reference. Is my understanding correct ?

Nov 14 '06 #1
9 2918

dragoncoder wrote:
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 behaviour of std::auto_ptr.
2. To implement a pkt::auto_ptr without auto_ptr_ref.
3. Check out the limitations of pkt::auto_ptr as compared to
std::auto_ptr (using steps 1 and 2).
4. Try to relate the concept of auto_ptr_ref to fill the gaps found in
step 3.

NOTE: Any siggestions on this are welcome.

As the first step I have written the following program.

=cat example.cxx
#include <iostream>
#include <memory>

using namespace std;

template <typename T>
auto_ptr <Tsource ( T a )
{
return auto_ptr <T( new T ( a ) );
}

template <typename T>
void print ( /* const */ auto_ptr <T>& p )
{
cout << "In print(): " << *p << endl;
}

int main()
{
print ( source ( 5 ) );
return 0;
}

z852378@premier :/home/z852378/rnd/pkt
=g++ example.cxx
example.cxx: In function `int main()':
example.cxx:20: could not convert `source(T) [with T = int]()' to `
std::auto_ptr<i nt>&'
example.cxx:14: in passing argument 1 of `void print(std::auto _ptr<T>&)
[with T
= int]'

Uncommenting the 'const' in print() definition compiles the code fine
and the output is also fine. Based on my current knowledge, this is
because the return value of source() is a temporary and therefore can
not be bound to a non-const reference. Is my understanding correct ?
yes.

Nov 14 '06 #2
dragoncoder wrote:
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.
Here is the original suggestion for the current auto_ptr:
http://www.open-std.org/jtc1/sc22/wg...1997/N1128.pdf

Carefully read the "Analysis of Conversion operations" section.

The following defect report on auto_ptr explains why the current
auto_ptr is partially broken and suggests a fully working auto_ptr:
http://www.open-std.org/jtc1/sc22/wg...ctive.html#463

Another interesting insight about auto_ptr, written by the most
authorized C++ core expert:
http://www.open-std.org/jtc1/sc22/wg...2000/n1232.pdf

Although I wrote defect report #463 I personally prefer to avoid
auto_ptr and to use policy based smart class *without* the copy w/
transfer of ownership semantics.

Rani

Nov 14 '06 #3
dragoncoder wrote:
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.
Did you read and understand the links given in this post:

http://groups.google.com/group/comp....262920e6f26428

If not, what is confusing you?

Cheers! --M

Nov 14 '06 #4

rani_shar...@ho tmail.com wrote:
dragoncoder wrote:
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.

Here is the original suggestion for the current auto_ptr:
http://www.open-std.org/jtc1/sc22/wg...1997/N1128.pdf

Carefully read the "Analysis of Conversion operations" section.

The following defect report on auto_ptr explains why the current
auto_ptr is partially broken and suggests a fully working auto_ptr:
http://www.open-std.org/jtc1/sc22/wg...ctive.html#463

Another interesting insight about auto_ptr, written by the most
authorized C++ core expert:
http://www.open-std.org/jtc1/sc22/wg...2000/n1232.pdf

Although I wrote defect report #463 I personally prefer to avoid
auto_ptr and to use policy based smart class *without* the copy w/
transfer of ownership semantics.

Rani
Thanks a lot for the references and it seems to me that these document
will answer my questions. But I would really love to have a look at the
changes done to auto_ptr since the beginning. I mean I want to know the
reason of the change in auto_ptr from CD-1 to CD-2. Could you please
give me some references on that ?

Thanks in advance.

Nov 14 '06 #5
Thanks everyone for the replies, I really appreciate it.

I have the following piece of text from "The C++ Standard Library" by
Nicolai Josuttis.

....
The rest of the class auto_ptr (auxiliary type auto_ptr_ref and
functions using it) consists of rather tricky conversions that enable
you to use copy and assignment operations for nonconstant auto_ptrs but
not for constant auto_ptrs...

I hope they are talking about the auto_ptr_ref trick here, correct me
if I am wrong. Anyways, this means it (the trick) enables me to use
copy and assignment operators for non-const std::auto_ptr (with the
help of auto_ptr_ref) but not for const auto_ptr. Now please look at
the following code.

// File pkt.h
#ifndef _PKT_H
#define _PKT_H

namespace pkt
{

template <typename T>
class auto_ptr
{
private:
// member which points to the allocated memory
T* ap;
public:
typedef T element_type;

// parameterized constructor
explicit auto_ptr ( T* p = 0 ) throw () : ap ( p ) { }

// default copy constructor
auto_ptr ( auto_ptr & a ) throw () : ap ( a.release() ) { }

// copy constructor with implicit conversion
template <typename Xauto_ptr ( auto_ptr <X>& a ) throw () : ap (
a.release() ) { }

// default assignment operator
auto_ptr& operator= ( auto_ptr & a ) throw ()
{
reset ( a.release() );
return *this;
}

// assignment operator with implicit conversion
template <typename XT& operator= ( auto_ptr <X>& a ) throw ()
{
reset ( a.release() );
return *this;
}

// destructor
~auto_ptr () throw ()
{
delete this->ap;
}

// returns the holder pointer
T* get () const throw ()
{
return this->ap;
}

// returns the object as an lvalue
T& operator* () const
{
return *(this->ap);
}

// returns the holder pointer
T* operator-() const
{
return this->ap;
}
// returns the holder pointer after deleting it
T* release () throw ()
{
T* tmp ( ap );
ap = 0;
return tmp;
}

// resets to the newly allocated memory
void reset ( T* a = 0 ) throw ()
{
if ( ap != a )
{
delete this->ap;
this->ap = a;
}
}

}; // end class auto_ptr
} // end namespace pkt

#endif // end #ifndef

// File pkt.cxx
#include <iostream>
#include <memory>
#include "pkt.h"

using std::cout;
using std::endl;
using pkt::auto_ptr;

template <typename T>
auto_ptr<Tsourc e ( T a )
{
return auto_ptr<T( new T ( ) );
}

template <typename T>
void print ( auto_ptr<T>& a )
{
cout << "Inside print(): value passed is: " << *a << endl;
}

int main()
{
auto_ptr<intpi1 ( new int ( 10 ) );
print ( pi1 ) ;
const auto_ptr<intpi2 ( new int ( 20 ) );
print ( pi2 );
}

=g++ pkt.cxx
pkt.cxx: In function `int main()':
pkt.cxx:26: no matching function for call to `print(const
pkt::auto_ptr<i nt>&)'

My point is that pkt::auto_ptr does not have any auto_ptr_ref trick but
then also it is allowing me to use the copy and assignment operator for
a non-const auto_ptr but not for const auto_ptr.

My question is what was the problem which was being tried to solve
using the auto_ptr_ref trick. An example would be much appreciated.

Thanks in advance.

Nov 14 '06 #6
I am totally lost with the readings that I have been doing, please help
me out. I thought auto_ptr_ref was introduces so that its possible to
return an std::auto_ptr from a function and copying it into another
std::auto_ptr. This is the code I tried.

=cat example.cxx
#include <iostream>
#include <memory>

using namespace std;

template <typename T>
auto_ptr <Tsource ( const T &a )
{
return auto_ptr <T( new T ( a ) );
}

int main()
{
auto_ptr <intpi2;
pi2 = source ( 5 );
return 0;
}

=g++ pkt.cxx
pkt.cxx: In function `int main()':
pkt.cxx:27: no matching function for call to
`pkt::auto_ptr< int>::auto_ptr(
pkt::auto_ptr<i nt>)'
pkt.h:23: candidates are: pkt::auto_ptr<T >::auto_ptr(pkt ::auto_ptr<X>&)
[with X
= int, T = int]
pkt.h:20: pkt::auto_ptr<T >::auto_ptr(pkt ::auto_ptr<T>&)
[with T
= int]
pkt.h:17: pkt::auto_ptr<T >::auto_ptr(T * = 0) [with T =
int]
pkt.cxx: In function `pkt::auto_ptr< Tsource(const T&) [with T =
int]':
pkt.cxx:27: instantiated from here
pkt.cxx:12: no matching function for call to
`pkt::auto_ptr< int>::auto_ptr(
pkt::auto_ptr<i nt>)'
pkt.h:23: candidates are: pkt::auto_ptr<T >::auto_ptr(pkt ::auto_ptr<X>&)
[with X
= int, T = int]
pkt.h:20: pkt::auto_ptr<T >::auto_ptr(pkt ::auto_ptr<T>&)
[with T
= int]

Based on what I have read, I expect the above code to get compiled. Can
someone please explain if I am missing somerhing ?

Thanks

Nov 14 '06 #7
* dragoncoder:
I am totally lost with the readings that I have been doing, please help
me out. I thought auto_ptr_ref was introduces so that its possible to
return an std::auto_ptr from a function and copying it into another
std::auto_ptr. This is the code I tried.
The error messages + your earlier postings indicate that it's absolutely
/not/ the code you tried.

[snip]

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '06 #8
Alf P. Steinbach wrote:
* dragoncoder:
I am totally lost with the readings that I have been doing, please help
me out. I thought auto_ptr_ref was introduces so that its possible to
return an std::auto_ptr from a function and copying it into another
std::auto_ptr. This is the code I tried.

The error messages + your earlier postings indicate that it's absolutely
/not/ the code you tried.
I am extremely sorry, I seem to have lost my senses while trying to
understand this beast. But I think I have pretty much got the idea.
This is the code which compiles fine with std::auto_ptr but not with
pkt::auto_ptr, which shows the missing link between these 2, which I
think is auto_ptr_ref.

=cat example.cxx
#include <iostream>
#include <memory>

using namespace std;

template <typename T>
auto_ptr <Tsource ( const T &a )
{
return auto_ptr <T( new T ( a ) );
}

template <typename T>
void print ( auto_ptr <T>& p )
{
cout << "In print(): " << *p << endl;
}

int main()
{
auto_ptr <intpi2;
pi2 = source ( 5 );
print ( pi2 );
auto_ptr <intpi1 ( source ( 10 ) );
return 0;
}

I am really surprised how pi2 = source ( 5 ) working with the help of
auto_ptr_ref ( or for that matter auto_ptr <intpi1 ( source ( 5 ) )
). Can someone please explain what is happening behind the scene ?

Thanks very much.

Nov 14 '06 #9
dragoncoder wrote:
>
I am really surprised how pi2 = source ( 5 ) working with the help of
auto_ptr_ref ( or for that matter auto_ptr <intpi1 ( source ( 5 ) )
). Can someone please explain what is happening behind the scene ?
1) pi2 = source ( 5 );
The code compiles because of operator=(auto_ ptr_ref<T>) that was added
to allow usage like the one that you mentioned.

This assignment operator was a late addition for auto_ptr that has been
accepted to TC1:
http://www.open-std.org/jtc1/sc22/wg...fects.html#127

2) const auto_ptr<int>& r = source(5)
The current standard requires a viable copy constructor in order to
bind a class rvalue (e.g. return value from function) to a const
reference since additional temporary might be involved.

The future standard will probably relax this (frustrating) requirement
to only allow direct binding, see:
http://www.open-std.org/jtc1/sc22/wg...fects.html#391

My suggestion (DR #462) for fixing auto_ptr actually seems to allow
this case with many highly compliant compliers.

Notice that the copy semantics in C++ can be extremely subtle and there
are several simple examples for which even highly compliant compliers
completely disagree (sometimes even inconsistent them self).

Personally, I prefer regulate the code with facilities that have "less"
subtleties, are more syntactically/semantically robust and easier to
understand.

Rani

Nov 15 '06 #10

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

Similar topics

1
1320
by: CID | last post by:
Hello all, I was reading about auto_ptr from TC++PL. It tells, the purpose of auto_ptr_ref is to implement the destructive copy semantics for ordinary auto_ptrs. Can somebody there please explain how it achives the requirement ? I am interested to know how auto_ptr_ref helps in transferring ownership and how it avoids copying of const auto_ptrs.
5
4334
by: Evgeny | last post by:
Hi, all! I try to convert my existing code written in VC6 to VC7 and have some problem with stl auto pointer template. class CColumn; #include <memory> typedef auto_ptr<CMyBase> CMyBasePtr; class CMyBase {
14
3588
by: Neelesh Bodas | last post by:
Hello All, My question is regarding the implementation of auto pointers - How does the class auto_ptr_ref work? eg: suppose I have : auto_ptr<int> foo(); int bar() {
10
4495
by: red floyd | last post by:
It seems that the use of auto_ptr<> is discouraged in many places in favor of boost::shared_ptr<> (or tr1::shared_ptr<>). But consider a PIMPL idiom, where there is a strict 1-1 relationship between interface objects and implementation object, and the implementation object lasts for the lifetime of the interface object. i.e. // header file class WidgetImpl; class Widget {
10
2630
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in section 4.2. The copy constructor is defined as: auto_ptr (auto_ptr& rhs) throw() : ap (rhs. release()) { }
39
864
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 Query("123");
1
2747
by: digz | last post by:
Hi, The std::auto_ptr 14.4.2 in Stroustrup ,the book talks about "std::auto_pt_ref is to implement destructive copy semantics" , after some more search I found that auto_ptr can be returned from function by value, and the class auto_ptr_ref is used to achieve that., beyond that the workings are not mentioned. Can someone help me understand, why is an auto_ptr_ref needed to return auto_ptr from a function, why is it different from a...
17
10067
by: Ankur Arora | last post by:
Hi All, I'm building a sample application that uses a custom auto_ptr implementation. The program crashes with the following output:- Output (Debug Assertion failed) ----------
0
9530
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.