forcing compile time function execution

Artur Skawina art.08.09 at gmail.com
Tue Dec 27 18:32:45 PST 2011


On 12/28/11 02:52, Timon Gehr wrote:
> On 12/28/2011 02:31 AM, Artur Skawina wrote:
>> So far the compiler isn't really helping:
>>
>>> Error: constraint IsPow2 is not constant or does not evaluate to a bool
>>
>> for:
>>
>> ---
>> bool IsPow2(alias N)() {
>>     if (!(N&  N-1))
>>        return 1;
>>     assert(0, N.stringof ~ " is not a power of two.");  // Abort compilation.
>> }
>>
>> auto POW2MASK(alias N)() if(IsPow2!N) {
>>    return cast(typeof(N))(N-1);
>> }
>> ---
>>
>> which works with "if(cast(whatever)IsPow2!N)" and "if(!!IsPow2!N)" as the constraint, but not without the cast...
>>
>> artur
> 
> Try if(IsPow2!N()){}.
> 
> I have filed a bug report:
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=7174

Adding "()" helps, and makes things work, just like with the explicit casts. 


But that was w/o property syntax enforcement; after turning on -fproperty i found this:

> Error: not a property IsPow2

for every POW2MASK "call" with a literal/enum POW2MASK arg.

IOW: every "POW2MASK!0U" results in "Error: not a property IsPow2", while using "POW2MASK!0U()" seems to work. Hmm.

artur


More information about the Digitalmars-d mailing list