January 03, 2015 Re: cannot modify struct with immutable members | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On 01/02/2015 09:07 PM, ketmar via Digitalmars-d-learn wrote: > structure instance with const fields can be initialized only once, upon > creation. so did `Test myTest1;` -- you initialized `myTest1` with > default values. you can't reinitialize it later. > > in C++ constness on member doesn't impose such restrictions. C++ has the same restriction: const members make objects unassignable: struct S { const int i; S() : i() {} }; int main() { S a; S b; a = b; } error: non-static const member ‘const int S::i’, can't use default assignment operator Ali |
January 03, 2015 Re: cannot modify struct with immutable members | ||||
---|---|---|---|---|
| ||||
Posted in reply to ted | On 01/02/2015 10:10 PM, ted wrote: > I'm now taking the view that const is there for the compiler to optimise > code on the basis that nothing can alter it once set (and can only be set > on initialisation). Of course, that is true for const values, not for const references. In the latter case it means "I will not modify but others may modify", so the optimization that you mention does not apply to const in general. However, immutable can be used for optimization. Ali |
January 03, 2015 Re: cannot modify struct with immutable members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli Attachments: | On Sat, 03 Jan 2015 01:00:58 -0800 Ali Çehreli via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > On 01/02/2015 09:07 PM, ketmar via Digitalmars-d-learn wrote: > > > structure instance with const fields can be initialized only once, upon > > creation. so did `Test myTest1;` -- you initialized `myTest1` with > > default values. you can't reinitialize it later. > > > > in C++ constness on member doesn't impose such restrictions. > > C++ has the same restriction: const members make objects unassignable: ah, thanks. it's a joy to forgetting C++ to this extent. ;-) |
January 03, 2015 Re: cannot modify struct with immutable members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Ali Çehreli wrote:
> On 01/02/2015 10:10 PM, ted wrote:
>
> > I'm now taking the view that const is there for the compiler to
> > optimise code on the basis that nothing can alter it once set (and can
> > only be set on initialisation).
>
> Of course, that is true for const values, not for const references. In the latter case it means "I will not modify but others may modify", so the optimization that you mention does not apply to const in general. However, immutable can be used for optimization.
>
> Ali
....thats (sort of) where I had started from.....
My (clearly incorrect) interpretation was that reinitialising 'myTest1' was part of the 'others may modify' category because of the fact that it was in scope of the _someFunc function....
...anyway....lots to learn.
BTW: your book is excellent, and as soon as you have it available for purchase I will certainly be buying a copy.
|
Copyright © 1999-2021 by the D Language Foundation