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

auto_ptr<> as function return

I am developing a type that protects ownership of data with non-const
ref copy-constructor and assignment operator similiar to auto_ptr<>. I
read that auto_ptr<> uses auto_ptr_ref to fascilitate returning from a
functoin and managed to get that working. However, when I try to assign
a value returned from this function, it doesn't work. I looked at the
auto_ptr<> implementation and don't see clearly how it is handling it
either. Anyways here's a condensed version of my code that illustrates
what I'm doing:

struct A {
struct ARef {
int* value;

ARef(int* v) : value(v)
{}
};

int* m_value;

A(int* value) : m_value(value)
{}

A(ARef ref) : m_value(ref.value)
{}

A(A& a) : m_value(a.m_value)
{}

operator ARef ()
{ return ARef(m_value); }

A& operator = (A& a)
{
m_value = a.m_value;
return *this;
}
};

A func()
{
return A(0);
}

int main()
{
A a(0);
a = func();
return 0;
}

I realize it's failing because a = func(); is using a temporary which
cannot be given to a non-constant reference such as A::operator=()'s
argument. The thing is I don't see how this is done in auto_ptr<> either
but yet that expression would work for it.
Jul 23 '05 #1
4 3776

"Kurt Stutsman" <ks*******@NOSPAM.sbcglobal.net> wrote in message
news:5i*****************@newssvr13.news.prodigy.co m...
I am developing a type that protects ownership of data with non-const ref
copy-constructor and assignment operator similiar to auto_ptr<>. I read
that auto_ptr<> uses auto_ptr_ref to fascilitate returning from a functoin
and managed to get that working. However, when I try to assign a value
returned from this function, it doesn't work. I looked at the auto_ptr<>
implementation and don't see clearly how it is handling it either. Anyways
here's a condensed version of my code that illustrates what I'm doing:

struct A {
struct ARef {
int* value;

ARef(int* v) : value(v)
{}
};

int* m_value;

A(int* value) : m_value(value)
{}

A(ARef ref) : m_value(ref.value)
{}

A(A& a) : m_value(a.m_value)
{}

operator ARef ()
{ return ARef(m_value); }

A& operator = (A& a)
{
m_value = a.m_value;
return *this;
}
};

A func()
{
return A(0);
}

int main()
{
A a(0);
a = func();
return 0;
}

I realize it's failing because a = func(); is using a temporary which
cannot be given to a non-constant reference such as A::operator=()'s
argument. The thing is I don't see how this is done in auto_ptr<> either
but yet that expression would work for it.


You don't say what about this is not working. Do you get a compiler error?

Why is the parameter for operator= non-const? You're not changing a, so why
not make it const?

-Howard
Jul 23 '05 #2
Howard wrote:

You don't say what about this is not working. Do you get a compiler error?
Yes, it's a compiler error. I've tested it with both g++ and comeau. g++
says:
test.cpp: In function `int main()':
test.cpp:38: error: no match for 'operator=' in 'a = func()()'
test.cpp:24: error: candidates are: A& A::operator=(A&)

func() returns an instance of A as a temporary which cannot be taken as
a non-const reference.
Why is the parameter for operator= non-const? You're not changing a, so why
not make it const?

It is a contrived example. In the real implementation, I do change A.
This code however shows enough to demonstrate the actual issue.
Jul 23 '05 #3
Kurt Stutsman wrote:
Howard wrote:

You don't say what about this is not working. Do you get a compiler
error?

Yes, it's a compiler error. I've tested it with both g++ and comeau. g++
says:
test.cpp: In function `int main()':
test.cpp:38: error: no match for 'operator=' in 'a = func()()'
test.cpp:24: error: candidates are: A& A::operator=(A&)

func() returns an instance of A as a temporary which cannot be taken as
a non-const reference.
Why is the parameter for operator= non-const? You're not changing a,
so why not make it const?


It is a contrived example. In the real implementation, I do change A.
This code however shows enough to demonstrate the actual issue.


No one has a solution for this? Or can at least explain how auto_ptr
does it?

Kurt
Jul 23 '05 #4
Well I found out that I was looking at the wrong header file on my
system (apparently I have 3 separate copies of memory in /usr/include).
I found there is an operator= that takes an auto_ptr_ref and explains
how "a = func();" works. Looking in the standard, I don't see that
operator specified for the std::auto_ptr<> spec though. Is this behavior
not guaranteed to work?
Jul 23 '05 #5

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

Similar topics

2
by: Siemel Naran | last post by:
This code fails compile std::auto_ptr<Base> f() { std::auto_ptr<Derived> out(new Derived()); return out; } There is ambiguity between a templated constructor and templated operator...
23
by: guru.slt | last post by:
Hi, see this code: auto_ptr<int> int_auto_p(new int(3)); auto_ptr<int> int_auto_p2(int_auto_p.get()); Then, both auto_pointer will own the same object. That will violate the single...
5
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; ...
2
by: Stephan Hoffmann | last post by:
Hi, I'm new to std::auto_ptr<> and wanted to use it with a base class and several derived classes. I'm using gcc 3.3.5 and get a compile error I don't know how to resolve when compiling the...
10
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...
4
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...
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:
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.