CTFE vs. traditional metaprogramming

Yigal Chripun yigal100 at gmail.com
Mon Oct 12 13:29:21 PDT 2009


On 12/10/2009 18:45, BCS wrote:
> Hello Michel,
>
>> On 2009-10-09 15:49:42 -0400, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> said:
>>
>>> Thanks!
>>>
>>> I plan to add more text at the end of the chapter that discusses the
>>> opportunities of CTFE. Walter revealed to me that CTFE, particularly
>>> now after it's been improved by leaps and bounds by Don and by Walter
>>> himself, could obviate a lot of the traditional metaprogramming
>>> techniques developed for C++.
>>>
>>> One question that bugs me is, where do you draw the line? Say there's
>>> a metaprogramming problem at hand. How to decide on solving it with
>>> CTFE vs. solving it with templates? It would be great to have a
>>> simple guideline that puts in contrast the pluses and minuses of the
>>> two approaches.
>>>
>>> It is quite possible that templates get relegated to parameterized
>>> functions and types, whereas all heavy lifting in metaprogramming
>>> should be carried with CTFE.
>>>
>> My idea on templates is that they're good when you have type
>> parameters, or to create types based on constant parameters.
>>
>> - - -
>>
>> But an interesting thing I realized in the last few months is this:
>> all you can do with a template you can also do at runtime provided
>> sufficient runtime reflection capabilities. Even creating types!
>> Details follow.
>>
>
> I'd like to forward the thought that runtime reflection and type
> creation is NOT a replacement for the same at compile time just as the
> compile time version is not a replacement for the run time version.
>
> Just to pick two differences: errors are forced to runtime and runtime
> types can't be inlined.
>
>

you can have compile-time errors with (static) asserts and you can 
inline runtime types by way of a smart JITer/VM and run-time profiling.
programs with long lifetimes like servers perform better in Java than in 
C++ because the VM optimizes the code at run-time.

it's analogous to DBs: you can write the implementation for your query 
yourself in c++ OR use a DB engine such as Oracle that collects 
statistics and optimizes your SQL query at run-time.

I think we had a discussion about this before. The only difference 
between compile-time and run-time is just that, the stage at which the 
code is optimized/run. compile time allows for ahead-of-time 
optimization (good for short lived programs and when you have 
ahead-of-time knowledge for performing the optimizations) while run-time 
allows for just-in-time optimization (more flexible for when you don't 
have ahead-of-time knowledge)



More information about the Digitalmars-d mailing list