News for hackers. Stuff that matters.
Re:Variadic Switch by PeterWhittaker on Wednesday May 14, 2025 @08:16PM (#43990544)
I find the article very interesting and informative but, honestly, of all of the approaches, I find the basic switch to be the most readable and likely the most maintainable, at least for this case.
Re:Variadic Switch by pjmlp on Thursday May 15, 2025 @04:49AM (#43993096)
The two major problems in C++, we as a comunity suffer from, are those that still insist using it as plain old C with some improvments, and those that do some kind of post-avant guard code, only understood by anyone coding every day in C++, that have as pastime reading ISO standard and compiler reference manuals, while attending C++ conferences.

One keeps the whole security discussion going on, while the other keeps an image that C++ is a language not worth learning.

Re:Variadic Switch by glouwbug on Thursday May 15, 2025 @12:35PM (#43996712)
Funny enough, the runtime switch, for all practical reasons, is probably just as fast
Re:Variadic Switch by cout on Friday May 16, 2025 @04:21AM (#44002978)
The limitation with the runtime switch is that it cannot be generated. If all you want to do is have different behavior for each type, then it's probably fine. But if you want to write generic code (apply the same function regardless of type), you need to be able to generate the function using metaprogramming techniques.

C++ isn't great for for metaprogramming, because much of it is a hack that uses the type system to do things it was never originally designed to do (i.e. as a lisp). But as metaprogramming has become more commonplace, the language has evolved with features that make it easier and more readable. It's still all based on a hack, and there's still no good way to debug a metaprogram. But at least I don't have to read the loki book anymore to grok it.

I still prefer to use a proper code generator when I can (it compiles faster, and I can see the generated code). Generating an ordinary switch outside of C++ is certainly an option and is something I've done. But I don't reach for it every time, because those tools tend to be kludgy as well (they can generate invalid C++, which templates and comstexpr cannot).