const in functions

Basile B. b2.temp at gmx.com
Mon Mar 13 15:50:18 UTC 2023


On Sunday, 12 March 2023 at 15:09:45 UTC, Salih Dincer wrote:
> Hi,
> [...]
> // A, we can get its to guarantee us that parameters
> // won't change:
>
> auto inConst(T)(T a, const T b) // const
> { // it's not needed --^     but     ^-- why can't this be used
>

Well you got the great answers to your questions already but I 
like to add a comment that is that

**`const` parameters without `ref` are really moot.**

I whish at least that they could give the guarantee to be rvalues 
(i.e have no address) but this is actually not the case. All the 
D compilers will actually copy `const` parameters to locals, 
making them lvalues, unless optimizations are requested(1).

See codegen : https://godbolt.org/z/vev1PGWh3.

This brings the idea that maybe a `rvalue` storage class could be 
added to the language.

(1): Actually DMD does not drop the copy **at all**, even with -O.



More information about the Digitalmars-d-learn mailing list