From: Eric Niebler (eric_at_[hidden])
Date: 2007-07-24 13:36:00
Matt Calabrese wrote:
> On 7/23/07, Eric Niebler <eric_at_[hidden]> wrote:
>> General design question here. This came up on the user's list, and it
>> made me question something I believed to be true. When writing a proxy
>> for, e.g., a std container, I see two options for handling const.
>> 0) Const-ness depends on the const-ness of the object being proxied:
>> 1) Const-ness depends on the proxy object itself:
> I suppose it really depends on context and how exactly you wish to deal with
> the proxy, but I would personally try to keep semantics as close as possible
> to a reference type.
Yes, I thought of this, but there is another viewpoint; namely, to make
proxy<T> behave as much like a T as possible. (I'm playing devil's
advocate here -- I don't espouse this view but I'm not convinced it's
wrong, either.) For instance, in generic code, you might overload a
foo() on const and non-const arguments:
template<typename T> void foo(T &t); // T is mutable
template<typename T> void foo(T const &t); // T is not mutable
In places like this, top-level const is used to determine mutability. So
if you have a proxy in the style of (1), it might end up in the wrong
overload. I don't generally write code like this, but my use of proxies
in the style of (0) has occasionally forced me to write a const overload
where it wouldn't otherwise be needed just to attract such proxies.
So, why in your opinion is the "proxy as reference" viewpoint to be
preferred over the "proxy as value" viewpoint? I know the answer in my
gut, but so far I haven't really heard a killer argument. Peter's
argument about copies of the proxy not being able to enforce their
const-ness is pretty convincing, though.
And this was Peter Dimov's counter example:
proxy<std::vector<int> > vec;
Maybe the author of Foo wants deep-const here: if a Foo object is const,
then the vector should be const as well. Again, I don't write code like
that, but I don't think there's anything wrong with it.
-- Eric Niebler Boost Consulting www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk