3 ms·
In 2020 there are some replacements for what used to be templates and even templates have improved to be more expressive. 17 years after this article was writte
by Hokusai 6y ago
In 2020 there are some replacements for what used to be templates and even templates have improved to be more expressive. 17 years after this article was written there seems to not be a better alternative. Templates are still at the core of C++ abstractions for the bad or the good.
- fwsgonzo 6y agoMuch less so with functions at compile time. The first thing I noticed was his example of the fibonacci numbers at compile-time. You can do those things with regular functions now, and just marking it as constexpr or constinit. I actually took the CRC32 algorithm and just slapped a template <int polynomial> and a constexpr on it, and called it CRC32 at compile-time. I'm using it for tons of string-hashing stuff where I want to replace a string with just a 32-bit number. It's incredibly efficient.
- layoutIfNeeded 6y agoHow does that look like in practice? I mean, you can’t use the table-based per-byte approach because you can’t store the lookup table with constexpr, so I guess you must be doing polynomial division via bit shifts and such, right?
- TeMPOraL 6y ago> you can’t store the lookup table with constexpr IIRC you can store a null-terminated string as constexpr, right? If so, then there's your lookup table.
- Google234 6y agoAlso I think absl has a constexpr map too.
- layoutIfNeeded 6y agoBut string literals are const, so you won’t be able to fill the lookup table based on the generator polynomial.
- fwsgonzo 6y agoThere is a table, here is the code: https://github.com/fwsGonzo/rvscript/blob/master/engine/script/util/crc32.hpp https://github.com/fwsGonzo/rvscript/blob/master/engine/scri...
- layoutIfNeeded 6y agoAh, true! I wasn’t aware that std::array was compatible with constexpr.
- sgerenser 6y agoI’ve done compile time crc32 string hashing for similar reasons with a raw array as a lookup table.
- tcbawo 6y agoIt seems that templates are becoming more of the core of C++ abstractions as the language picks up functional idioms and people are moving away from overusing/abusing object oriented designs. Concepts are making templates much accessible and easier to work with. Link time optimization helps reduce some of the template bloat and virtual call overhead. As much as people seem to be ragging on C++ lately, it keeps getting better and better.
- klipt 6y ago> 17 years after this article was written there seems to not be a better alternative Circle is a pretty cool alternative for C++ metaprogramming: https://www.circle-lang.org https://www.circle-lang.org it's just a prototype though, so who knows if it will ever become standard.