Bug? Const Class Makes Constructor Const

Vijay Nayar madric at gmail.com
Thu Oct 25 20:39:34 UTC 2018


On Thursday, 25 October 2018 at 20:27:43 UTC, Steven 
Schveighoffer wrote:
>
> See, you should be calling it with argument types `() const`, 
> but you are using `()`.
>
> It's a terrible way to say "you can only call this on a const 
> object", but constructors are special, so I think a special 
> error message should be used.
>
> To answer your other question -- yes it is correct that const 
> is applying to the constructor when you apply it to the type.
>
> -Steve

Oh, I see now.
   auto b = new const(Bob)();

I suppose const(Bob) is a type in this case.  I guess I'm not 
sure what the purpose of having a const constructor is. I see the 
benefit of having const members, to assure that the object is not 
modifed after creation, but when the constructor is const as 
well, that enforces head-const behavior as well, so that whenever 
a reference variable is initialized, that it cannot later 
reference something else.

Maybe it's just best that I avoid this feature and don't put 
const in front of the class.


More information about the Digitalmars-d mailing list