When does template instantiation bloat matter in practice?

前端 未结 5 819
忘了有多久
忘了有多久 2021-01-02 00:56

It seems that in C++ and D, languages which are statically compiled and in which template metaprogramming is a popular technique, there is a decent amount of concern about t

相关标签:
5条回答
  • 2021-01-02 01:36

    There's little problem in C++, because the amount of template stuff you can do in C++ is limited by their complexity.

    In D however ... before CTFE (compile-time function evaluation) existed, we had to use templates for string processing. This is also the reason big mangled symbols are compressed in DMD - the strings used as template arguments become part of the mangled symbol names, and when instantiating templates with longer segments of code (for instance) the resulting symbol sizes would spectacularly explode the object format.

    It's better nowadays. But on the whole, templates still cause a lot of bloat for a simple reason - they parse faster and are more powerful than in C++, so people naturally use them a lot more (even in cases that technically wouldn't need templates). I must admit I'm one of the prime offenders here (take a look at tools.base if you like, but be sure to keep a barf bag handy - the file is effectively 90% template code).

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

    I think it's mainly mental bloat. The next programmer to work on your code will first need to figure out what subset of it matters.

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

    Template bloat is NOT an issue (It is a mental problem not a code problem).

    Yes it can get big. But what's the alternative?
    You could write all the code yourself manually (once for each type). Do you think writting it manually will make it smaller. The compiler only instanciate the versions it actually needs and the linker will remove multiple copies spread over compilation units.

    So there is no actual bloat.
    It is just building what you use. If you use a lot of different types you need to write more code.

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

    I think you'll need to find an older compiler to see the template code bloat in practice. Modern C++ compilers (and linkers) have been able to optimize it away for a while.

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

    Template instantion bloat IS a matter in practice, because it can increases ( a lot!!! ) compile and link time.

    I personnaly thinks that c++ #1 problem is compil time, and it's mainly due to template.

    I worked on a project with about 50 libs. We had our own rtti system using templates. I had to rewrite because of the template bloat

    Here is some numbers:

    • libs went from 640 mbytes to 420 mbytes
    • temps went from 4.3 gbytes to 2.9 gbytes
    • full rebuild went from 19:30 to 13:10
    0 讨论(0)
提交回复
热议问题