3 ms·
One of the reasons I dislike boost is in the first code sample. What’s the point of implementing what boils down to a 10 line function (as shown in the decompil
by cmovq 18d ago
One of the reasons I dislike boost is in the first code sample. What’s the point of implementing what boils down to a 10 line function (as shown in the decompiled output) like this
typedef xor_combine_engine<
xor_combine_engine<
linear_feedback_shift_engine<uint32_t, 32, 31, 13, 12>, 0,
linear_feedback_shift_engine<uint32_t, 32, 29, 2, 4>, 0>, 0,
linear_feedback_shift_engine<uint32_t, 32, 28, 3, 17>, 0> taus88;
- mitxela 18d agoBoost predates C++11
- ainiriand 18d agoC++ is uglier than punching one's dad.
- windenntw 18d agoCorrect, but also beside the point. To do it in almost-plain C, you'd need fairly complex macros that are more difficult to write correctly. To do it in plain C without macros, or plain C++ without templates... you'd need to work out the combination of the template expansion yourself and write down a piece of code that is more likely to have bugs and more difficult to understand.
- eru 18d agoI'm not quite sure: you could probably use plain C functions, and a reasonably smart compiler could see through it all and inline and duplicate and merge the code as necessary. EDIT: I just had an AI agent run the experiment. At least for my version of clang, they produce the same assembly for x86_64 (modulo using slightly different registers).
- windenntw 18d agoThis is called template-based compile-time programming. By making 3 instances of linear_feedback_shift_engine class template ( and 2 of xor_combine_engine ), you are forcing the compiler to expand the code exactly as-is 3 times, each with different parameters. The parameters to the template are constant, therefore the compiler can easily look at how they are used and you are guaranteed ( even in a 1999 c++ compiler ) that the compiler will look at the copies of the code and merge them as much as possible into a single piece of code... which is the one that you see when you decompile the code. So in summary, it means that you get to write fairly readable code while the final binary is fully optimized as-if you had spent the time merging all the variants as needed for the specific constants.
- eru 18d agoYou could get something similar from CPP macros. But the compiler wouldn't be able to help you as much.
- cmovq 17d agoMy point is that the straight-line non-templated function presented later in the article is significantly more readable, it’s like 8 lines of code with a few shifts and xors.
- deterministic 17d agoI completely agree. Every time I remove boost from projects the build times drop dramatically and it usually doesn't take much work to swap the boost code for something else that isn't template fetish art.