Const sucks

Regan Heath regan at netmail.co.nz
Tue Sep 11 02:58:48 PDT 2007


Regan Heath wrote:
> Assuming there is even a requirement to make select members of a struct 
> (initially declared without const) const (if there isn't we have no 
> problem) then using a template seems the most sensible solution.  It 
> would also allow you to apply const or tail const where appropriate. eg.

Talking to myself, a sure sign of mental instability ;)

The same applies to making select members of a class const.  I think 
it's important to remember that we're talking about _modifying_ an 
existing class/struct definition _adding_ const where it was not initially.

This would only be required in cases where:
1. you need different const rules for instances of the same class/struct 
in the same version of the code.
2. you need to const correct a 3rd party API.

In any other case you can simply apply const to the class/struct 
definition directly, no need for a template to do it. eg.

struct Something
{
   const(*Foo) pFoo;
   const int a;
   int b;
}

There will be no need to _modify_ this with a template because it will 
always need to have a mutable reference to a const Foo and that 
requirement won't be different for some instances and not others.  It 
may change in future versions of the code, but in that case a simple 
change to the struct definition is all you need, not a template.

I think the ability to tail const a class reference with something like:

class Foo { int a; }
const(*Foo) pFoo;

Handles 99% of the cases which we should realistically be worried about.

Regan



More information about the Digitalmars-d mailing list