Added copy constructors to "Programming in D"

Mathias LANG geod24 at gmail.com
Thu Feb 10 08:06:23 UTC 2022


On Thursday, 10 February 2022 at 02:39:11 UTC, Ali Çehreli wrote:
>
> Yeah, that issue bugs me a little.
>
> Ali

I think an *immediate* improvement we could make to ease people's 
life is to make `auto` peel the outermost qualifier level inside 
functions.

So that:
```D
const int* ptr;
auto p2 = ptr;
static assert(is(typeof(p2) == const(int)*));
```

I really can't think of any downside to it, only upsides:
- It is still predictable / consistent;
- It *might* reduce the number of template instantiations in some 
cases;
- It just flows more naturally... If you want full constness, 
there's still `const`;

Likewise, I've been tinkering with having `const* p2 = ptr` to 
allow `p2` to be mutated. But it has far more implications and 
the benefits aren't as clear. For `auto`, I think the case is 
pretty straightforward.

Fun fact: C++ mangling actually does that.
```D
extern(C++) void foo (const int a);
extern(C++) void bar (int a);

pragma(msg, foo.mangleof); // _Z3fooi not _Z3fooKi
pragma(msg, bar.mangleof); // _Z3bari
```

P.S: Thanks for spreading the love about `-preview=in`. I haven't 
moved forward with enabling it by default yet, because it just 
does exactly what we want it to do and has never bothered us, so 
we tend to forget it's not the default. I shall however start to 
move ahead now that the switch has been available for a few 
releases.


More information about the Digitalmars-d-announce mailing list