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.val ue)
{}
A(A& a) : m_value(a.m_val ue)
{}
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. 4 3799
"Kurt Stutsman" <ks*******@NOSP AM.sbcglobal.ne t> wrote in message
news:5i******** *********@newss vr13.news.prodi gy.com... 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<> implementati on 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.val ue) {}
A(A& a) : m_value(a.m_val ue) {}
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
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.
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
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
conversion, according to my compiler. Seems there are too many constructors
and operator conversions. But this code works:
|
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 ownership expectation on auto_pointer.
|
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
{
|
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 following (in file testInteger.cc):
line 38: std::auto_ptr<HInteger> d(new HInteger(5));
line 39: std::auto_ptr<HObject> e(new HObject());
|
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()) {
}
| |
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...
|
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...
|
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,...
|
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,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |