const and immutable values, D vs C++?

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


I also find the following behaviour a bit unclear:

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

T mk(T)(){
     T tmp = {2019, null};
     return tmp;
}

node1 mk_node1(){
     node1 tmp = {2019, null};
     return tmp;
}

node2 mk_node2(){
     node2 tmp = {2019, null};
     return tmp;
}


void main() {
     immutable x0 = mk!node0();  //succeeds?
     immutable x1 = mk!node1();  //succeeds?
     immutable x2 = mk!node2();  //succeeds?
     immutable y1 = mk_node1();  //fails
     immutable y2 = mk_node2();  //succeeds, so only first child 
has to be immutable?
}



More information about the Digitalmars-d-learn mailing list