From: Paul Bibbings on
Below is a toy class that I had posted some while ago in a response to
comp.lang.c++ and which attempts to model a `reseatable reference'. In itself
it is unimportant, offered merely to aid discussion, but I did think that I
could simplify it under C++0x using a deleted copy assignment operator, only to
find out that I was wrong.

template<typename T>
class ReseatableRef
{
public:
explicit ReseatableRef(T& t): t_ptr(&t) { }
ReseatableRef& operator=(const T& t)
{
*t_ptr = t;
return *this;
}
ReseatableRef& operator=(const ReseatableRef& other)
{
*t_ptr = *other.t_ptr;
return *this;
}
ReseatableRef& reseat(T& t)
{
t_ptr = &t;
return *this;
}
operator T&() { return *t_ptr; }
operator T() const { return *t_ptr; }
T * operator&() const { return t_ptr; }
private:
T *t_ptr;
};

You will see that I have implemented the semantics of the copy assignment
operator to be that of /value/ assignment only, so that the `reseating' requires
a specific call the the member function reseat(T&). The definition of the copy
assignment operator is added for that reason because, of course, to omit it
would require the compiler to generate the default copy assignment operator with
the wrong semantics.

Looking ahead to deleted functions in C++0x, I then thought that I might be able
to `delete' this function rather than implement it, relying on the conversion
operator to permit op=(const T&) to perform the assignment with the same
semantics. However, with gcc-4.4.3 I find that this is not tried and that the
compiler merely chokes on trying to invoke a deleted function.

22:19:49 Paul Bibbings(a)JIJOU
/cygdrive/d/CPPProjects/CLCPPM $cat reseatable_ref.cpp

template<typename T>
class ReseatableRef
{
public:
explicit ReseatableRef(T& t): t_ptr(&t) { }
ReseatableRef& operator=(const T& t)
{
*t_ptr = t;
return *this;
}
ReseatableRef& operator=(const ReseatableRef&) = delete;
// ...
operator T&() { return *t_ptr; }
operator T() const { return *t_ptr; }
// ...
private:
T *t_ptr;
};

typedef ReseatableRef<int> iref_t;

int main()
{
int i, j;
iref_t i_ref(i);
iref_t j_ref(j);
i_ref = j_ref;
}

22:19:57 Paul Bibbings(a)JIJOU
/cygdrive/d/CPPProjects/CLCPPM $gcc -std=c++0x -c reseatable_ref.cpp
reseatable_ref.cpp: In function 'int main()':
reseatable_ref.cpp:13: error: deleted function 'ReseatableRef<T>&
ReseatableRef<T>::operator=(const ReseatableRef<T>&) [with T = int]'
reseatable_ref.cpp:29: error: used here

I am guessing this is what I /should/ have expected?

To explain my thinking, however, I was starting from the understanding that the
following works...

class A {
public:
A(int i) : i_(i) { }
void set(const int& i) { i_ = i; }
operator int() const { return i_; }
private:
int i_;
};

int main()
{
A a1(1), a2(2);
a1.set(a2);
}

.... with the conversion operator being invoked /because/ A::set(const A&) is
*not* available. I imagined that that was the effect of a deleted copy
assignment operator, that it merely made it "not available, so try something
else." What it seems to mean, however, is "it's not available, period!"

Regards

Paul Bibbings

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On Jun 27, 3:56 pm, Paul Bibbings <paul_bibbi...(a)googlemail.com>
wrote:

> Looking ahead to deleted functions in C++0x, I then thought that I might be able
> to `delete' this function rather than implement it

You can already do that in C++03. Just make the function private and
undefined.

> relying on the conversion
> operator to permit op=(const T&) to perform the assignment with the same
> semantics. However, with gcc-4.4.3 I find that this is not tried and that the
> compiler merely chokes on trying to invoke a deleted function.

Indeed, that is because deleted functions still get picked up by
overload resolution, and then get rejected.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]