1 matches bool, 2 matches long

Rob T alanb at ucora.com
Mon Apr 29 10:04:27 PDT 2013


On Monday, 29 April 2013 at 14:08:20 UTC, Mike James wrote:
>> gdc:
>>
>> bool x = false;
>> x++;
>>
>> main.d:50: Error: operation not allowed on bool 'x'
>>
>> why not? is just an integer after all. another special case?
>
> If you are going to create a boolean then use it as a boolean - 
> it's not an integer any more. Don't mix and match - there's 
> nothing worse than trying to follow some code that uses a 
> variable in one way then, out of lazyness, uses it in a 
> different way.
>
>>
>> this works:
>>
>> int x = false;
>> x++;

The main point made in this thread is that because bool is not 
really an integral type, you cannot use it as one, but D 
overloads integral types with bool which is clearly wrong. You 
also cannot in general interchange ints and bools inside a 
template without special conditions to differentiate between them 
the two (eg ++bool fails), therefore bool should not overload on 
ints or implicitly cast to/from ints and bools under most 
situations.

--rt


More information about the Digitalmars-d mailing list