Boost logo

Boost :

Subject: Re: [boost] [array] assignement operator
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2009-05-15 08:24:21


Joseph Gauterin wrote:
On Friday, May 15, 2009 7:52 AM
> On Fri, May 15, 2009 at 12:28 PM, vicente.botet
> <vicente.botet_at_[hidden]> wrote:
>
> > Boost.Array defines an assignement operator with the
> following prototype

[snip expected copy assignment operator]

> > I was wondering if this can be extended to manage with
> arrays of different sizes, having as effect the copying of
> the minimum of N and M elements from the source to the target.
> >
> > template<typename T, std::size_t N>
> > class array {
> > public:
> > /...
> > template<typename U, std::size_t M>
> > array& operator=(const array<U, M>& rhs);
> > // Effects:std::copy(rhs.begin(),rhs.begin()+min(N,M), begin())
> > };
> >
> > Is there anything wrong with this interface?
>
> I think such an assignment operator is counter-intuitive and its use
> will lead to hard to read code. I'd expect two objects to be
> equivalent after one is assigned to the other.

This is absolutely the case. That's the point of copy assignment operators, std::auto_ptr and a few other classes notwithstanding! For example, Rogue Wave provides a substring class for their string class that uses the copy assignment operator to update the referenced string, not to make the destination substring equivalent to the source. While it provides a convenient interface, it also leads to surprises, particularly for the uninitiated.

> I certainly wouldn't
> expect an array on the LHS to still contain some of its original
> elements after a smaller RHS array is assigned to it.

array is unusual in that the elements begin with indeterminate values when not constructed from an initializer list, and size() always returns N, so its behavior is unusual for a container however you look at it. Retaining the original values is the only possible behavior for such a function when the source is larger than the destination. It would also be surprising to find only a subset of a larger source in the destination when using a copy assignment operator, but array doesn't grow.

The suggested idea may have its uses, but it has sufficiently odd behavior that it ought not to be a member function and certainly not the copy assignment operator. Perhaps a free function is in order.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk