[phobos] phobos commit, revision 2181

spir denis.spir at gmail.com
Fri Nov 19 02:22:47 PST 2010


On Thu, 18 Nov 2010 14:27:41 -0800
Andrei Alexandrescu <andrei at erdani.com> wrote:

> That should not be accepted, so you made the right call.
> 
> Andrei
> 
> On 11/18/10 2:23 PM, Shin Fujishiro wrote:
> > By the way, is it acceptable to swap structs that have const member(s)?
> > std.algorithm.swap() used to allow the following code:
> > ----------
> > struct S
> > {
> >      const string name;
> > }
> > auto a = S("a");
> > auto b = S("b");
> >
> > swap(a, b);
> > assert(a.name == "b";
> > assert(b.name == "a");
> > ----------
> >
> > This behavior breaks const correctness, whereas it's sometimes useful
> > since S doesn't allow built-in assignment.  Should swap() disallow it?

Hum, is this really a transgression of const's promise? Each object's .name fields remains unchanged as expected. Rather only the symbolic relations (var-id <--> object) change. It's another kind of change.
Here, the 2 facts that the fields are called 'name' and that they hold values equal to var-ids, commonly called 'name' as well, just introduce confusion.
What about this, is it also inacceptable?

struct S
{
    const uint code;
}
auto a = S(1); // a <--> obj1 (code:1)
auto b = S(1); // b <--> obj2 (code:2)

swap(a, b);
assert(a.code == 2); // a <--> obj2 (code:2)
assert(b.code == 1); // b <--> obj1 (code:1)

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the phobos mailing list