Metaprogramming in C++ and in D

后端 未结 10 1912
生来不讨喜
生来不讨喜 2021-01-30 04:46

The template mechanism in C++ only accidentally became useful for template metaprogramming. On the other hand, D\'s was designed specifically to facilitate this. And apparently

相关标签:
10条回答
  • 2021-01-30 05:39

    I believe nothing is better qualified to show the incredible power (TM) of the D template system than this renderer I found years ago:

    The compiler output

    Yes! This is actually what is generated by the compiler ... it is the "program", and quite a colourful one, indeed.

    Edit

    The source seems to be back online.

    0 讨论(0)
  • 2021-01-30 05:39

    Well in D you can easily impose static constraints on template parameters and write code depending on the actual template argument with static if.
    It's possible to simulate that for simple cases with C++ by using template specialization and other tricks (see boost) but it's a PITA and very limited cause the compiler doesn't expose many details about types.

    One thing C++ really just can't do is sophisticated compile time code generation.

    0 讨论(0)
  • 2021-01-30 05:42

    Just to counter the D ray tracing post, here is a C++ compile time ray tracer (metatrace):

    enter image description here

    (by the way, it uses mostly C++2003 metaprogramming; it would be more readable with the new constexprs)

    0 讨论(0)
  • 2021-01-30 05:50

    There are quiet a few things you can do in template metaprogramming in D that you cannot do in C++. The most important thing is that you can do template metaprogramming WITHOUT SO MUCH OF A PAIN!

    0 讨论(0)
提交回复
热议问题