Is Template Metaprogramming faster than the equivalent C code?

前端 未结 8 2363
旧时难觅i
旧时难觅i 2021-01-02 02:07

Is Template Metaprogramming faster than the equivalent C code ? ( I\'m talking about the runtime performance) :)

相关标签:
8条回答
  • 2021-01-02 02:37

    The answer is it depends.

    Template metaprogramming can be used to easily write recursive descent language parsers and these can be inefficient compared to a carefully crafted C program or a table-based implementation (e.g. flex/bison/yacc).

    On the other hand, you can write metaprograms that generate unrolled loops which can be more efficient than a more an conventional C implementation that uses loops.

    The main benefit is that metaprograms allow the programmer to do more with less code.

    The downside is that it also gives you a gatling gun to shoot yourself in the foot with.

    0 讨论(0)
  • 2021-01-02 02:37

    I worked on a project where another programmer had tried out metaprogramming. It was terrible. It was a complete headache. I'm an average programmer with a lot of C++ experience, and trying to devise what the hell they were trying to do took way more time than if they had written it straight out to begin with.

    I'm jaded against C++ MetaProgramming because of this experience.

    I'm a firm believer that the best code is most easily readable by an average developer. It's the readability of the software that is the #1 priority. I can make anything work using any language... but the skill is in making it readable and easily workable for the next person on the project. C++ MetaProgramming fails to pass muster.

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