Handmade Hero » Forums » Code » Using templates for data structure library
boagz57
Jason
94 posts
#17173 Using templates for data structure library
3 months, 1 week ago Edited by Jason on Jan. 8, 2019, 1:09 p.m. Reason: Initial post

I'm currently trying to create my own data structure library of sorts for my C++ projects. Because of this I will want all my structures to be type agonistic. I know typically this is done using C++ templates (Array<int>, Vector<float>) but I know templates often get slack for there slow compilation times. Although, from what I've gathered from other posts on this site, it seems most of the compilation issues with templates arise from trying to do too much with them (using them for metaprogramming) or just overusing them in general (such as in the std library where many nested template classes occur). My question is would templates used in this manor (meaning only being used for simple type generics and not any meta-programming craziness) still cause enough compilation problems for me to consider finding an alternative solution? To be clear, I want compile times similar to Casey's (between 1sec - 10sec) for my programs for which I will generally be building as single compilation units.

Edit:

I've found a site which claims templates can still be faster than using macros in this regard. I still have yet to check this out myself. Not sure if anyone else has things they do for generics that are different from either solution.
Croepha
David Butler
79 posts / 1 project

I love pretty much anything technical, whether it be programming, networking, hacking or electronics.

#17176 Using templates for data structure library
3 months, 1 week ago

This is one point where my opinion differs form most people here.

I tend to use C++ templates quite liberally, but I strictly avoid using the STL except for some rare cases... I have a few things that I have implemented with templates, like linked lists, dynamic arrays, string formatting stuff and a few others that are designed around my specific programming style. For me these were a big win. I have also probably spent about the same amount of time trying to do something in templates and finding out that I've created a monster and just wasted a bunch of time, for these it might be better to not use meta-programming or just use more straightforward code generation methods... I recognize that C++ templates are a less than ideal method of meta-programming, but I still find that they have occasional utility... Its probably only worth looking into using a template if its something that you will probably use very often...

I have not noticed that my use of templates causes slow compilation. For my style I tend to use several translation units, and use incremental compilation. My compilation times are typically less than 500ms. From my experience, the real cause for slow compilations is the huge size of headers that are often found in C++ Template libraries, every time I #include an STL header it adds like 100ms - 200ms per include...

So, its not really about using templates or not, is really about amount of code that the compiler has to ingest...

Also, I exclusively use Clang, on all platforms... it may be possible that templates are slower on other compilers, I really couldn't say...

Also, what I would suggest is just timing your build times, Clang has some built in methods of giving you statistics, also if you are on Linux you can use /usr/bin/time to give you some info, also Ninja (the build tool that I use) show you how long specific parts took to build... Pay attention to your build times and see how they change as your code changes...


404 Signature not found
boagz57
Jason
94 posts
#17180 Using templates for data structure library
3 months, 1 week ago

Thanks for your insight. That is encouraging. I'll probably try using templates but will keep my eye out on the compilation cost (if any).