News for hackers. Stuff that matters.
Re:C++20 Idioms for Parameter Packs by CoastalCoder on Tuesday February 27, 2024 @09:41AM (#39524678)
My 2 cents: it's a bad idea because it's hard to test / debug.

C++ template / overload resolution involves some very convoluted logic. And it can take some work to even notice if / how it went differently than a programmer expected.

Programmers typically have logging, interactive debugging, etc. to investigate complicated algorithms like this. But (almost?) no C++ compiler provides such tooling for investigating the behavior of this algorithm.

IMHO it's an area where C++ tooling could serious use improvement.

Re:C++20 Idioms for Parameter Packs by nix0n on Tuesday February 27, 2024 @10:55AM (#39525545)
This is why the C preprocessor is superior to templates: gcc's "-E" option will show you the preprocessed source file. (I think clang also?)

My personal theory is that compiler authors have not provided a template equivalent, because they are embarrassed about the code that's being produced.

(If you disagree, prove me wrong!)

Re:C++20 Idioms for Parameter Packs by planede on Tuesday February 27, 2024 @12:06PM (#39526493)
cppinsights.io shows expanded function templates as explicit specializations. It's built on LLVM, but I don't think that this tool is upstreamed.

https://cppinsights.io/lnk?code=dm9pZCBmb28oYXV0bykge30KCnZv...

Re:C++20 Idioms for Parameter Packs by nix0n on Tuesday February 27, 2024 @12:22PM (#39526694)
Thank you! This is exactly the sort of thing I was looking for.

I found the source at https://github.com/andreasfertig/cppinsights

Re:C++20 Idioms for Parameter Packs by gpderetta on Tuesday February 27, 2024 @11:05AM (#39525699)
> prove me wrong

Template don't "produce code". They get expanded well after parsing. There is no code to be embarrassed for.

Also you can ask GCC to dump any intermediate state: https://gcc.gnu.org/onlinedocs/gcc/Developer-Options.html.

Re:C++20 Idioms for Parameter Packs by nix0n on Tuesday February 27, 2024 @11:26AM (#39525971)
Is there a specific "-fdump" that you recommend?
Re:C++20 Idioms for Parameter Packs by actionfromafar on Tuesday February 27, 2024 @09:58AM (#39524864)
Yes! I never thought of it that way, but imagine having a real nice IDE with a "compilation debugger", showing all these things unfolding in real-time as you change a function.
Re:C++20 Idioms for Parameter Packs by logicchains on Tuesday February 27, 2024 @02:01PM (#39528124)
You have static_assert for testing/debugging. The lovely thing about compile-time programming is you don't even need a separate test stage; the compiler runs your tests (asserts) for you. Then if the code builds you know it's correct.

This pattern is getting easier and easier in modern C++ as more and more thing are made constexpr.