Latest const expansion

Regan Heath regan at netmail.co.nz
Tue Sep 11 08:42:07 PDT 2007


I'm interested in what people think of the idea(s) I had for expanding 
on Walter's latest const proposal and handling head/tail const on class 
references.

Ideas originally posted as replies, here:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=58107
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=58112

Also here:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=58114
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=58115


In short, Where:

head const - means the class reference itself cannot be modified
tail const - means the class members cannot be modified

Where T is a class:

const(T)  indicates both head/tail const
const(*T) indicates tail const
const(&T) indicates head const

Eg.

class A { int a; }

const(A)  a;  //a cannot be modified, a.a cannot be modified
const(*A) b;  //a can be modified, a.a cannot be modified
const(&A) c;  //a cannot be modified, a.a can be modified

The idea being that const(*T) means const("value of the type T") where 
the value of a class reference is the class instance itself.  Likewise 
const(&T) means const("reference/pointer of the type T") indicating the 
reference is to be const.

For template/meta-programming purposes the syntax "const(*T)" and 
"const(&T)" should be legal for T where T is a value type, i.e. struct, 
union, and primitives (excluding pointers) but will be identical in 
behaviour to "const(T)"


For pointers and arrays the "const(*T)" (tail const) syntax is not 
required as we can say:

const(T)[]
const(T)*

but perhaps for meta-programming purposes it should be legal, eg.

const(*char[])
const(*char*)


For pointers and arrays the "const(&T)" (head const) syntax is required 
as there is currently no way to get a const array reference to mutable 
data, so:

const(&char[])
const(&char*)

would specify a const array reference and const pointer respecitvely 
both to mutable 'char' data.

Thoughts?  Too complicated?  :P

Regan



More information about the Digitalmars-d mailing list