473,763 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

auto_ptr_ref

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()
{
auto_ptr<int*> k = foo();
}

The exact sequence of calls as I see here is (no optimizations assumed)
-
1) foo returns auto_ptr<int> by value
2) CC is called on the return value.
3) The returned auto_ptr<int> is converted to auto_ptr_ref<in t> by
using operator auto_ptr_ref
4) Now since this is a copy initialization and not a direct
initialization, the RHS (whose type is now auto_ptr_ref<in t>) needs to
get converted to auto_ptr<int> again - done by ctor of auto_ptr<int>
which takes auto_ptr_ref<in t>
5) Now CC should be called - but again we are back to old problem - the
generated auto_ptr is temporary and hence cannot be passed as a
non-const reference to the function.

Seems I am going somewhere in cycle :-(

Josuttis' book doesnot say enough on this -

<quote>
"The mechanism relies on a slight difference between the overloading
and template argument deduction rules. This difference is too subtle to
be of use as a general programming tool, but it is sufficient to enable
the auto_ptr class to work correctly."
</quote>

Can somebody explain what I am not getting here?

Nov 23 '05 #1
14 3590
* Neelesh Bodas:

eg: suppose I have :

auto_ptr<int> foo();

int bar()
{
auto_ptr<int*> k = foo();
}

The exact sequence of calls as I see here is (no optimizations assumed)
-
1) foo returns auto_ptr<int> by value
OK.

2) CC is called on the return value.
No, there is no std::auto_ptr( std::auto_ptr const& ) copy constructor.

3) The returned auto_ptr<int> is converted to auto_ptr_ref<in t> by
using operator auto_ptr_ref
OK.

4) Now since this is a copy initialization and not a direct
initialization, the RHS (whose type is now auto_ptr_ref<in t>) needs to
get converted to auto_ptr<int> again - done by ctor of auto_ptr<int>
which takes auto_ptr_ref<in t>
OK.

5) Now CC should be called
No, you've reached the goal.

- but again we are back to old problem - the
generated auto_ptr is temporary and hence cannot be passed as a
non-const reference to the function.


What generated auto_ptr? The one generated is the one you've named 'k'.

--
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 23 '05 #2
Hi Alf, Thanks for the help.

Actually, more I think on this, more confusion it is creating.

auto_ptr<int> foo();

int bar()
{
auto_ptr<int*> k = foo();
}

Isn't is correct that whever a function returns by value, a CC must get
called on the return value(Though it might be optimized away by the
compiler?)

No, there is no std::auto_ptr( std::auto_ptr const& ) copy constructor.


I believe that we do not need a std::auto_ptr( std::auto_ptr const& )
copy constructor - std::auto_ptr( std::auto_ptr& ) would do the work -
since the source of the copy inside "foo" is not necessarily a const :

std::auto_ptr<i nt> foo()
{
std::auto_ptr<i nt> v;
return v; // a copy ctor will be called on v since this is return by
value
}

But when I debugged throgh std::auto_ptr code I didnot find any call to
the given copy constructor (as you said in the reply.)

I am not getting where exactly I am going wrong.

Nov 23 '05 #3
* Neelesh Bodas:
Hi Alf, Thanks for the help.

Actually, more I think on this, more confusion it is creating.

auto_ptr<int> foo();

int bar()
{
auto_ptr<int*> k = foo();
}
Isn't is correct that whever a function returns by value, a CC must get
called on the return value(Though it might be optimized away by the
compiler?)
No. _Some_ constructor must be called. That call can be optimized
away, but the constructor must be available to be called (the as-if rule
for optimization).

No, there is no std::auto_ptr( std::auto_ptr const& ) copy constructor.


I believe that we do not need a std::auto_ptr( std::auto_ptr const& )
copy constructor - std::auto_ptr( std::auto_ptr& ) would do the work -
since the source of the copy inside "foo" is not necessarily a const :

std::auto_ptr<i nt> foo()
{
std::auto_ptr<i nt> v;
return v; // a copy ctor will be called on v since this is return by
value
}

But when I debugged throgh std::auto_ptr code I didnot find any call to
the given copy constructor (as you said in the reply.)


That is unfortunately a different effect.

Here the reference-to-non-const copy constructor could be called, and I
believe _is_ called at the formal level (the C++ abstract machine
level), but that call is optimized away by your compiler.

However, declare 'v' as const, or use a temporary, and you're no longer
in possible copy constructor call territory: _some_ constructor must be
called (although it can be optimized away), but in the case of const or
temporary auto_ptr that constructor isn't the copy constructor.

I am not getting where exactly I am going wrong.


This time it was mistaking the compiler's optimization as confirmation
of a misinterpretati on of what I wrote (sorry about not being more
clear). I didn't mean that a copy constructor is never called for any
function result. I meant that a copy constructor does not always have
to be called, and in particular I meant, and wrote, that std::auto_ptr
doesn't have a ref-to-const copy constructor, which is the usual copy
constructor used for a function result.

--
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 23 '05 #4
Hello Alf,

Alf P. Steinbach wrote:
No. _Some_ constructor must be called. That call can be optimized
away, but the constructor must be available to be called (the as-if rule
for optimization).


Thanks, this clears my misunderstandin g.

Continuing on my previous mail, we see that there are _two_ user
defined conversions going on one after the other in a single expression
-

1) std::auto_ptr<i nt> to std::auto_ptr_r ef<int> by a call to
std::auto_ptr<i nt>operator auto_ptr_ref<in t>()
2) Call to the converting constructor
std::auto_ptr<i nt>(std::auto_p tr_ref<int>) to create the result of type
auto_ptr<int>

If I understand right, since both of these are user defined
conversions, only one of them is allowed.

Could you please comment on this?

Nov 23 '05 #5
* Neelesh Bodas:
* Alf P. Steinbach:

No. _Some_ constructor must be called. That call can be optimized
away, but the constructor must be available to be called (the as-if rule
for optimization).


Thanks, this clears my misunderstandin g.

Continuing on my previous mail, we see that there are _two_ user
defined conversions going on one after the other in a single expression
-

1) std::auto_ptr<i nt> to std::auto_ptr_r ef<int> by a call to
std::auto_ptr<i nt>operator auto_ptr_ref<in t>()
2) Call to the converting constructor
std::auto_ptr<i nt>(std::auto_p tr_ref<int>) to create the result of type
auto_ptr<int>

If I understand right, since both of these are user defined
conversions, only one of them is allowed.

Could you please comment on this?


I'd rather not... ;-)

Because it's an issue that people tend to have to agree to disagree on.

My personal take is that the second doesn't really count as a user
defined conversion because it's not a conversion producing a temporary;
it's like plugging a char const* into a std::string constructor with
direct initialization syntax (see below); no conversion has to be found
by the compiler, it's a direct match.

But that is just a convenient shovel-it-under-the-carpet-and-wave-
yer-hands-frantically explanation, because if you do

class Base {};
class Derived: public Base {};

std::auto_ptr<B ase> x = std::auto_ptr<D erived>( new Derived );

then you _are_ up against the single conversion rule, and the only thing
different seems to be use of a templated constructor.

On the other hand (or is it the third, now), if you write the
initialization as a direct initialization,

std::auto_ptr<B ase> x( std::auto_ptr<D erived>( new Derived ) );

then with a standard-conforming compiler all is a good (note here: MSVC
7.1 is not a standard-conforming compiler in this respect).

Perhaps someone who Really Really Knows could be kind enough to comment
on the formal "why" your point (2) isn't counted in the number of
user-defined conversions involved -- or perhaps that it is but that
the standard library is allowed to do anything, whichever is the case.

--
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 23 '05 #6
Alf P. Steinbach wrote:
then you _are_ up against the single conversion rule, and the only thing
different seems to be use of a templated constructor.


I observed a similar behaviour with a non-templated constructor also -

class Y { } ;
class X {
public:
X() { }
X (X&) { } // copy constuctor takes non-const reference
operator Y();
X(Y) { }
} ;

X foo() { X p; return p; }

int main()
{
X k = foo();
}

Here -
1) foo returns an object of type X by value. This is a temporary.
2) since this temporary cannot be passed as a reference-to-non-const,
hence the copy constructor is of no use here
3) So operator Y() is called on this temporary, and it returns another
object of type Y.
4) Now again the converting constructor X(Y) will be called and the
resulting value is put into k.

Since this is a copy initialization (and not a direct initialization) ,
I was thinking that this involved _two_ calls to user defined
conversion operators. But still this compiles (Comeau online, g++
3.4.2)

Nov 23 '05 #7

Neelesh Bodas wrote:
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()
{
auto_ptr<int*> k = foo();
}


as you know, it's not possible to pass RHS "foo()" as non const
reference "auto_ptr &".
but you can call it's not const methods "foo().nonconst ()".
so, "auto_ptr" use that fact to allow constructor modify RHS.

as illustration
#include<iostre am>

struct auto_ptr {
int n;
static int next () { static int i; return ++i; }

struct auto_ptr_ref {
auto_ptr * ptr;
auto_ptr_ref ( auto_ptr * p ) : ptr (p) {}
};

auto_ptr () : n (next()) { std::cout << this << ":auto_ptr():ne w(" <<
n << ")" << std::endl; }
~auto_ptr () { std::cout << this << ":auto_ptr():de lete(" << n << ")"
<< std::endl; }
auto_ptr ( auto_ptr & );
auto_ptr & operator = ( auto_ptr & );

auto_ptr ( auto_ptr_ref /*by value*/ r ) {
this->n = r.ptr->n;
r.ptr->n = 0; // update RHS
std::cout << this << ":auto_ptr(auto _ptr_ref):" << r.ptr <<
std::endl;
}
operator auto_ptr_ref () {
std::cout << this << ":operator auto_ptr_ref(): " << std::endl;
return auto_ptr_ref(th is);
}

};

auto_ptr foo () { auto_ptr ptr; return ptr; }

int main(){
auto_ptr ptr = foo ();
}

Nov 23 '05 #8
Hi Aleksey,
thanks for the reply.

Aleksey Loginov wrote:
auto_ptr ( auto_ptr_ref /*by value*/ r ) {
this->n = r.ptr->n;
r.ptr->n = 0; // update RHS
std::cout << this << ":auto_ptr(auto _ptr_ref):" << r.ptr <<
std::endl;
}
operator auto_ptr_ref () {
std::cout << this << ":operator auto_ptr_ref(): " << std::endl;
return auto_ptr_ref(th is);
}

};

auto_ptr foo () { auto_ptr ptr; return ptr; }

int main(){
auto_ptr ptr = foo ();
}


The basic reason why I am not able to understand why this works is that
I observe _two_ user defined conversions applied here -
1) conversion from auto_ptr to auto_ptr_ref using operator auto_ptr_ref
() : applied on return value of foo
2) conversion from auto_ptr_ref to auto_ptr using the constructor
auto_ptr ( auto_ptr_ref) applied on the result of 1.

The reason why I am considering (2) as a conversion is that the
initialization of ptr is done as a copy initialization - not a direct
initialization.

Or may be it is the case that (2) above is _not_ considered as a
conversion by the compiler somehow. Not sure.

Hence the confusion.

Nov 23 '05 #9
Neelesh Bodas wrote:
Hello All,
My question is regarding the implementation of auto pointers - How
does the class auto_ptr_ref work?

[snip]

See these links by N. Josuttis and S. Meyers, respectively:

http://www.josuttis.com/libbook/auto_ptr.html

http://www.awprofessional.com/conten...tr_update.html

Cheers! --M

Nov 23 '05 #10

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

Similar topics

3
1933
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
1
1322
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.
14
1885
by: lutorm | last post by:
Hi, I'm having a problem with a return statement being parsed to return a function (I think). Check here: template <typename T> class A {}; template <typename T> class maker_of_A { public: A<T>& make_A() {return A<T>();}; };
14
3496
by: Andrew | last post by:
Hello all: After spending some time figuring out auto_ptr class' implementation, I decided to write a small article detailing its use of the auto_ptr_ref proxy class to enable construction and assignment from temporaries. I have placed the manuscript at: http://www.nd.edu/~ahenrick/auto_ptr.pdf It develops the problem and its solution in an incremental manner, which will hopefully make it accessible for most people without being
10
1745
by: Ivan Vecerina | last post by:
Here's a relatively simple code snippet: #include <memory> class Base { public: Base(); virtual ~Base(); virtual void f(int a, char const* name);
9
2921
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 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).
1
2749
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...
18
2454
by: Barry | last post by:
struct A { void Print() const { cout << "Print" << endl; } }; auto_ptr<AGet() {
4
2378
by: james.lawton | last post by:
Hi, I'm having a problem that I can't diagnose. I'm creating istreams of unknown types, and want to manage them on the stack, co I'm passing around ownership-holding pointers. Usually, I would use std::auto_ptr<std::istream>, but it seems to be deallocating early, as the call to read(...) below breaks. I've condensed a test case. Using my own pointer works fine, but using auto_ptr does not (see USE_AUTO_PTR). I solved the issue by a...
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
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
9998
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...
0
9822
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
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
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

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.