We had a corporate modern C++ training with some committee member. On day 2, somebody in slack just flat out asked the trainer "is the committee making all of this complex on purpose so they can sell training?" The guy laughed and said great joke.
Yes. It was not supposed to be funny. It accurately reflected frustration that this room of senior developers with 15+ year experience on average has with modern C++
edit: I hope the committee member survived getting 1 negative comment during the course of his $10.000 per day lecture
It accurately reflected frustration that this room of senior developers with 15+ year experience on average has with modern C++
Maybe the problem lies as much with the room as with C++ then.
Would you go back to using C++98? You can, it's right there. You can ignore everything from the last 18 years if you like, all the compilers have a '98 mode. Or set the compiler version to 11 and ignore the last 15 years. Or... etc... Does "the room" do that? I suspect not, because you have invited someone to talk about the new features, which means you're probably on a reasonably up to date compiler.
What this all comes down to is some sort of rehashed thing along the lines of "anything I care about is important anything I don't personally care about is unnecessary complexity". This is not useful. And certainly not if the only comment is a barb with nothing behind it.
edit: I hope the committee member survived getting 1 negative comment during the course of his $10.000 per day lecture
That's not a negative comment, it's a nasty comment wrapped up as a question. I'm sure the committee member survived and you can walk away glad someone made his day worse.
But what's the point? You didn't achieve anything except making yourself feel slightly better at someone else's expense.
the frustration in the room aligns with frustration I see online (e.g. here).
"the room" uses /std:c++latest and implements new C++ features the morning after MSVS update delivers it.
Nobody considers new features unnecessary complexity - those would be easy to ignore. It is that we want to use them, but run into billion issues, like coroutines without necessary primitives, baffling syntax like requires requires, oversights like inability to forward declare concepts, weird stuff like operator <=> needing an #include, etc. And the training was just that, introducing a feature and then going over billion weird edge cases and counterintuitive traps and how to avoid them.
But what's the point? You didn't achieve anything except making yourself feel slightly better at someone else's expense.
I did not make the comment. I did not say I agree with the comment. I did not say it was the only comment. But I do certainly understand the frustration behind it. I told the story to illustrate how an average C++ joe feels where I work
the frustration in the room aligns with frustration I see online (e.g. here).
Yeah and what does just being nasty to a committee member achieve except making the world a slightly worse place?
Nobody considers new features unnecessary complexity - those would be easy to ignore. It is that we want to use them, but run into billion issues,
The problem with the programmer in the room is the programmer often assumes he's the smartest man in the room and that his vague, half formed thought combined with usually angry opinions are somehow facts.
What you're after is a language which is both perfectly ergonomic, very efficient and yet completely backwards compatible with a very simple 1970s language with basically no features. The assumption here is that the committee have not been able to deliver with it because they are at best foolish or at worst venal.
That is what your co-worker is saying.
Nobody considers new features unnecessary complexity
Yeah and no one considers backwards compatibility unnecessary complexity. And no one considers native performance unnecessary complexity.
And how do you square that circle? Being a jerk to someone trying isn't going to make the problem any easier.
but run into billion issues
Have you/your coworker/the room ever bothered to read about why those are the case, or just decided to not find out, assume you (etc) could do it better and then made someone's life just a bit worse to score some points?
I told the story to illustrate how an average C++ joe feels where I work
The average programmer IME feels frustrated at lots of things. Frankly this "angry certainty" is pervasive among programmers. Not just C++ ones, it's there across the board. Angry at the language, hardware, editor, other programmers (all of whom are fools), managers, customers, deadlines, code reviews, lack of code reviews, etc etc etc etc.
It's really tiresome.
And also, why should I care? Sure the "average joe" is frustrated at C++, but I'm damned if I know how to fix any of those problems. You don't know and he certainly doesn't know. So what does being a jerk about it achieve apart from flagging an unpleasant workplace?
It’s a template for, meaning that it is fully unrolled at compile time. Ideally it would create a switch statement, but any compiler worth its salt will optimize switch statements and such sequences of if statements into one or more array lookups.
I would add a note that a template for and a switch can provide slightly different machine code output, even with all optimizations enabled. I discuss that a little bit here in a blogpost, where I change a switch statement to a template for in order to take advantage of reflection.
GCC ends up generating very similar code, but orders it a little bit differently. From some fiddling I came to the conclusion that it has to do with how GCC tries to assume the likely/unlikely paths differently, since GCC makes some assumptions about the likeliness of paths from how you order your switch cases, and it makes different assumptions for a sequence of if statements, which a template for will expand to. And that affects how GCC decides to arrange the machine code.
GCC's the only compiler with a mainline reflection implementation, and it's the compiler which is relevant for my project in that blogpost. So I didn't really check anything else. But it would seem the experimental Clang reflection branch also orders the machine code slightly different for a switch and a template for of if statements: https://compiler-explorer.com/z/xz4bx71or
Whether that's because it too has different assumptions for the likeliness of certain paths, I couldn't say.
Honestly, iterating all the values with template for (which is instantiating a bunch of branches, not actually iterating) is probably the best way to do this.
We have tools to do a LOT more than getting a string from an enum value because this was implemented like this. It may make the trivial case a bit less trivial but it makes more complicated cases possible.
It's like saying "why do we have to build a string formatting system to write std::print("Hello World");". Simply printing hello world isn't the point. simply printing the value of an enum isn't the point of reflection, it's just something reflection can do easily.
For you to write an exhaustive switch statement, you, the programmer writing the code, must iterate through the enum and write each branch.
template for is exactly the same thing that your brain is doing. It's also accurate 100% of the time and doesn't make human mistakes like forgetting a branch because.
Why are we trying so hard to contort the meaning of “iterate” in this thread? This is getting ridiculous.
Aside from that, I don’t know what gave you the impression that I don’t prefer the template for implementation. The original critique was obviously about the apparent linear time complexity of the template for implementation, but the reality is that compilers will exploit consecutive enum values to achieve as close to constant time complexity as possible. switch statements, whose origins lay in jump tables, are slightly more explicit about this, but it doesn’t really matter in optimized builds. I just think it would be nice if one could use template for to generate switch statements.
See my other comment. switch statements are absolutely not a form “iteration” in any useful sense of the word, but regardless, I believe the original commenter misinterpreted template for as a for loop executed at runtime, or at least that consecutive enum values would not be grouped.
Ideally one could create switch statements with reflection, but any modern compiler will convert the resulting sequence of if statements to one or more array lookups.
The article is ok but very basic, with the most seen example ever: enum to string... taken from a example from the paper? I do not remember but I definitely saw this code elsewhere.
:D
But I guess it can be informative for someone that knows nothing about reflection.
Any my intuition tells me that sort of code generation isn't supported without some token injection mechanism, but I'd need someone like /u/barryrevzin to confirm.
From a skim of the standard wording, I'm not actually seeing something that should disqualify this from working. After all, the equivalent expanded statement should work fine, even if it's weird that the case labels are in different scopes.
But my standardese certainly isn't the best, so I could well be missing something.
class A
{
int b = 10;
public:
static auto builder()
{
return makeBuilder<A>();
}
int getB() const {
return b;
}
private:
void withFoo(int a) {
b += a;
}
void withBar(int a, int c) {
b += a + c;
}
};
int main() {
auto a = A::builder()
.withFoo(10)
.withBar(20, 10)
.build();
std::cout << a->getB() << std::endl;
return 1978;
}
260
u/lucidbadger May 12 '26
It's when people try to make sense of modern C++ and then sit and reflect staring at the wall contemplating their life choices.
42
u/Climbing_Silver May 12 '26 edited May 12 '26
Surely there should be a better way to get a string from an enum value than iterating through all the values?
Edit: Didn't know how "template for" works. Thanks for the clarification y'all.
12
u/germandiago May 12 '26
The article is ok but very basic, with the most seen example ever: enum to string... taken from a example from the paper? I do not remember but I definitely saw this code elsewhere.
:D
But I guess it can be informative for someone that knows nothing about reflection.
6
u/pdp10gumby May 12 '26
sadly it’s too late to get this called CTTI for symmetry with the much-reviled RTTI
5
u/dgendreau May 13 '26
I must insist that we all come together in calling ^^ the Batman operator.
2
u/anotherprogrammer25 May 13 '26
Thanks for the article. Good explanation and examples.
3
u/hicklc01 May 12 '26
I know compilers may convert to the same but I'd prefer to be more explicit in the use of a switch statement
1
u/VinnieFalco wg21.org | corosio.org May 13 '26
Look in the mirror.
1
u/Huge-Presentation810 May 20 '26 edited May 24 '26
Builder pattern:
https://compiler-explorer.com/z/cY68nTdPz
Can be made more simple?
-2
u/Dry-Entry5201 May 12 '26
I only know mirror reflection.
-3
u/LongestNamesPossible May 12 '26
Goodbye modules, you've had it too good for too long.