copying const struct
Benjamin Thaut
code at benjamin-thaut.de
Mon Oct 14 04:38:23 PDT 2013
Am 14.10.2013 13:35, schrieb Jack Applegame:
> Why this doesn't compile?
>
> http://dpaste.dzfl.pl/21ef5b04
>
> class Foo {}
>
> struct Bar1 {
> const(Foo[]) member;
> }
>
> struct Bar2 {
> const Foo member;
> }
>
> void main() {
> const Bar1 bar1;
> const Bar2 bar2;
> Bar1 b1 = bar1; // ok
> Bar2 b2 = bar2; // cannot implicitly convert expression (bar2) of
> type const(Bar2) to Bar2
> }
Because in D const is transitive. That means if a reference is const the
object it points to is also const. And everything that object points to
is const, and so on.
The line "Bar2 b2 = bar2;" would remove const from "member" which does
not happen because const is not convertible to mutable implicitly.
Kind Regards
Benjamin Thaut
More information about the Digitalmars-d-learn
mailing list