On Mon, 09 Apr 2012 13:51:17 -0400, Jacob Carlborg <[email protected]> wrote:
On 2012-04-09 17:30, Steven Schveighoffer wrote:
// untested
inout(Foo) clone() inout {
return new inout(Foo)(data);
}
Note, you can't post-assign data, since inout is effectively const
inside an inout function.
-Steve
Ok, that works. But then I want to modify the clone:
void bar (const Foo foo)
{
auto c = foo.clone();
c.data = ...
}
But now "c" is const. I don't want "c" to be const, I just want "foo" to
be const.
Then c.data cannot be the same reference as foo.data.
Counter-case:
void bar( const Foo foo)
{
auto c = foo.clone(); // assume this works;
*(cast(int*)c.data) = 6; // note even though I'm casting, there is no
removal of const, so this should be defined behavior.
}
immutable int i = 5;
const foo = new Foo(&i);
bar(foo);
assert(i == 5); // oops, it's 6!
In that case, you have to clone the data as well.
-Steve