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.