 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
glenlow@pixelglow.com Guest
|
Posted: Wed Jul 27, 2005 3:30 pm Post subject: Const operator= in proxy class? |
|
|
Hi All
Consider a typical proxy class:
template <typename T> class proxy
{
operator T () const;
proxy& operator= (const T& rhs);
};
Such an object might be returned by an iterator dereference or a matrix
indexer access for example, and would be the moral equivalent (if not
exact synactic equivalent) of a C++ reference.
Some people (James Kanze, Sep 2004 in this group?) seem to favor
declaring the operator= as a const member function e.g.
proxy& operator= (const T& rhs) const;
What are the pros and cons of each approach?
Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Jul 28, 2005 11:14 am Post subject: Re: Const operator= in proxy class? |
|
|
[email]glenlow (AT) pixelglow (DOT) com[/email] wrote:
| Quote: | Consider a typical proxy class:
template <typename T> class proxy
{
operator T () const;
proxy& operator= (const T& rhs);
};
Such an object might be returned by an iterator dereference or
a matrix indexer access for example, and would be the moral
equivalent (if not exact synactic equivalent) of a C++
reference.
Some people (James Kanze, Sep 2004 in this group?) seem to
favor declaring the operator= as a const member function e.g.
proxy& operator= (const T& rhs) const;
What are the pros and cons of each approach?
|
First, "favor" isn't really the correct word. If you asked me
why I declared it const, I'd answer "because I can". If I'm not
mistaken, I started declaring it const when I saw that Scott
Meyers did so. (The most important thing for a good software
engineer is to know who to copy from:-).) On the whole, I think
I rather like the const because it flags that something unusual
is going on; if you see an assignment operator declared const,
you know it isn't just the usual, run of the mill assignment
operator. But in a code review, if someone hadn't declared it
const, I wouldn't bother mentionning the point unless there was
a house rule about it.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
tony_in_da_uk@yahoo.co.uk Guest
|
Posted: Fri Jul 29, 2005 12:44 am Post subject: Re: Const operator= in proxy class? |
|
|
Hi Glen,
A basic design decision is whether a const proxy should indicate that
the controlled object is to be deemed const, such that it can't be
modified through the proxy. This seems a natural enough model.
Implementation's something like:
class Proxy
{
public:
Proxy(T& t) : t_(t) { }
operator const T&() const { return t_; }
operator T&() { return t_; }
Proxy& operator=(const T& rhs) { t_ = rhs; return *this; }
private:
T& t_;
};
Why does your proxy return a copy of the controlled object in its
casting operator, rather than providing access by reference? Doesn't
this create an unnamed temporary, preventing things like
"my_proxy_to_int *= 2;" even if the proxy isn't const? Is there a
special case where this is useful?
Cheers,
Tony
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
glenlow@pixelglow.com Guest
|
Posted: Sat Jul 30, 2005 10:12 am Post subject: Re: Const operator= in proxy class? |
|
|
Yes but not declaring it const has some benefits -- a const proxy then
gets treated differently from a non-const proxy, much like a normal C++
const reference gets treated differently from a non-const reference.
E.g.
template <typename T> class array
{
const proxy <T> operator[] (int index) const;
proxy <T> operator[] (int index);
};
So I was wondering what the benefits are for the other approach,
declaring proxy::operator= const.
Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
glenlow@pixelglow.com Guest
|
Posted: Sat Jul 30, 2005 10:20 am Post subject: Re: Const operator= in proxy class? |
|
|
| Quote: | Why does your proxy return a copy of the controlled object in its
casting operator, rather than providing access by reference? Doesn't
this create an unnamed temporary, preventing things like
"my_proxy_to_int *= 2;" even if the proxy isn't const? Is there a
special case where this is useful?
|
So that the proxy doesn't surrender control of the controlled object.
(Especially important for operator T& () which allows the caller to do
anything to the controlled object without going through the proxy
again.)
Or when there's no discrete controlled object per se e.g. the proxy of
vector <bool>, which has no discrete bool object. So operator= and
operator T do something other than straight assignment and value copy.
Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
glenlow@pixelglow.com Guest
|
Posted: Sat Jul 30, 2005 10:20 am Post subject: Re: Const operator= in proxy class? |
|
|
| Quote: | Why does your proxy return a copy of the controlled object in its
casting operator, rather than providing access by reference? Doesn't
this create an unnamed temporary, preventing things like
"my_proxy_to_int *= 2;" even if the proxy isn't const? Is there a
special case where this is useful?
|
So that the proxy doesn't surrender control of the controlled object.
(Especially important for operator T& () which allows the caller to do
anything to the controlled object without going through the proxy
again.)
Or when there's no discrete controlled object per se e.g. the proxy of
vector <bool>, which has no discrete bool object. So operator= and
operator T do something other than straight assignment and value copy.
Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Aug 01, 2005 10:25 am Post subject: Re: Const operator= in proxy class? |
|
|
[email]glenlow (AT) pixelglow (DOT) com[/email] wrote:
| Quote: | Yes but not declaring it const has some benefits -- a const
proxy then gets treated differently from a non-const proxy,
much like a normal C++ const reference gets treated
differently from a non-const reference.
E.g.
template <typename T> class array
{
const proxy <T> operator[] (int index) const;
proxy <T> operator[] (int index);
};
So I was wondering what the benefits are for the other
approach, declaring proxy::operator= const.
|
That's an interesting idea: using the const-ness of the proxy to
determine whether it can modify the actual object. I actually
adopted my current idiom before it was decided what const meant,
if anything, on a return value, and have gotten into the habit
of providing two proxies, one const, and one not.
Note that the above solution does allow the user to break const,
by declaring an explicit instance of proxy. Since proxy is
copiable:
array<t>::proxy p = a[ i ] ;
should do the trick, even if a is const. I rather doubt that
this is a real problem, however; I can't imagine anyone
accidentally doing it, and of course, if the intentional goal is
to break const-ness, then a const_cast will do the trick just as
well.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|