Compile time function execution...

Lutger lutger.blijdestijn at gmail.com
Thu Feb 15 15:09:07 PST 2007


Andrei Alexandrescu (See Website For Email) wrote:

> Bill Baxter wrote:
>> Walter Bright wrote:
>>> Walter Bright wrote:
>>>> This should obsolete using templates to compute values at compile time.
>>>
>>> For contrast, compare with the C++ proposal:
>>> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1972.pdf
>> 
>> It's kinda long and boring, but it looks like the key differences are
>> 
>> 1) The need to tag compile-time functions with a new keyword,
>> "constexpr", though they seem to sell this as an advantage.  "a
>> programmer can state that a function is intended to be used in a
>> constant expression and the compiler can diagnose mistakes." -- page 9.
>> 
>> 2) The restriction that a constexpr function can only contain "return"
>> followed by exactly one expression.  No loops for you!  And I hope you
>> like quadruply nested b?x:y expressions!
> 
> 3) The C++ feature is applicable to user-defined types.
> 
> Andrei

These user-defined literals seem useful too. Would it be hard to implement
this with structs, or are there perhaps more subtle issues? Here is an
earlier article it was based on:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1511.pdf

Btw this is a really cool feature, how it is done in D I mean. A little
while ago someone posted a CachedFunction template to do memoization on any
function, this is now easy to do 100% safe, right? Or at least for the 
subset of functions that pass the compile-time criteria.




More information about the Digitalmars-d mailing list