Bug? Const Class Makes Constructor Const

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


On Thursday, 25 October 2018 at 19:51:45 UTC, Steven 
Schveighoffer wrote:
> On 10/25/18 3:23 PM, Vijay Nayar wrote:
>> void main()
>> {
>>    const class Bob {
>>      this() {
>>        bob = null;
>>      }
>>      Bob bob;
>>    }
>> 
>>    Bob b = new Bob();
>> }
>> 
>> Compiling this program gives the following error:
>> 
>> onlineapp.d(10): Error: constructor `onlineapp.main.Bob.this() 
>> const` is not callable using argument types `()`
>> 
>> I know that declaring a 'const class' adds 'const' to every 
>> member of the class, but does that even include constructors? 
>> I'm not sure how one could use a class with a const 
>> constructor. Is this a bug?
>
> Generally:
>
> const(Bob) /*or auto*/ b = new const(Bob)();
>
> But the error message there looks really confusing to me.
>
> -Steve

Yeah it's a bit odd.  The error message isn't super clear, the 
only reason I think it's because "const" is applying to the 
constructor, is because I get the same error by manually applying 
const to all members like below.

void main()
{
   class Bob {
     this() const {
       bob = null;
     }
     const Bob bob;
   }

   auto b = new Bob();
}

But if the "this() const {" is changed to "this() {", then the 
error goes away.


More information about the Digitalmars-d mailing list