On 12/28/2011 02:31 AM, Artur Skawina wrote:
On 12/28/11 01:29, Jonathan M Davis wrote:
On Tuesday, December 27, 2011 18:59:55 Artur Skawina wrote:
Is there a way to *force* CTFE? Without using an extra wrapper like this:

auto f_impl(alias a)() { / * ... ctfeable ... */ }
auto f(alias a)() { enum ctfed = f_impl!a; return ctfed; }

A "@compile" (or "@ctfe" etc) function attribute would eliminate the need
for the wrapper...

The _only_ time that CTFE is done is when it _must_ be done. So, if you want
something to be evaluated at compile time, you need to assign it to a value
which must be initialized at compile time. e.g. an enum or a static variable.

I know. What i wanted was a way to force it *w/o* that assignment (the 'f()' 
wrapper above that i wanted to avoid).

I still think a @ctfe attribute would be useful, but Timon's suggestion to use 
template constraints to execute ctfe functions will help in at least some 
cases. Still trying to get used to the idea of running code from template 
constraints. :)

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

Reply via email to