[Issue 18561] postblit behaves inconsistently with constants
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Mar 6 12:56:29 UTC 2018
https://issues.dlang.org/show_bug.cgi?id=18561
ag0aep6g at gmail.com changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
CC| |ag0aep6g at gmail.com
Resolution|--- |DUPLICATE
--- Comment #1 from ag0aep6g at gmail.com ---
(In reply to Ajieskola from comment #0)
> Postblits behave inconsistently with constants. As shown in the second
> example, it cannot modify members which are declared as const. But if that
> is wrong, the first one should compile neither, as all the members should be
> treated as const when the whole variable is const.
The first example shouldn't compile.
`title = title.dup;` doesn't do any actual harm, because you're not altering
the original. But consider `title[0] = 'W';`. Now you're changing the original
`title` to "Wondon bridge", and you're doing it through a `const` reference.
That should not be possible. Even worse, the original title could be
`immutable`:
----
import std.stdio;
struct placeAtWorldMap
{ char[] title;
this(this)
{ title[0] = 'W'; /* ! */
}
}
void main()
{ immutable char[] title = "London bridge".dup;
const place = const placeAtWorldMap(title);
const samePlace = place;
title.writeln; // Wondon bridge
}
----
Issue 18357 already covers that problem. Closing as DUPLICATE. Feel free to
revert if you think it's not an exact duplicate.
*** This issue has been marked as a duplicate of issue 18357 ***
--
More information about the Digitalmars-d-bugs
mailing list