const and immutable values, D vs C++?

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Thu Dec 5 08:31:41 UTC 2019


On Wednesday, 4 December 2019 at 23:27:49 UTC, Steven 
Schveighoffer wrote:
> void foo(alias f)()
> {
>    alias ConstType = const(typeof(f()));
>    pragma(msg, ConstType);
> }

I expressed myself poorly, what I am interested in is this:

struct node1 {int value; const(node1)* next;}
struct node2 {int value; immutable(node2)* next;}

alias node = node1; // may swap to node2

node f(){
     node tmp = {2019, null};
     return tmp;
}

void main() {
     // choose readonly as immutable if x can be cast as such
     // otherwise choose const
	readonly x = f();
}



More information about the Digitalmars-d-learn mailing list