Live data from Hacker News

C++ : if constexpr isn't broken

brevzin.github.io

1–10 of 83 posts

Re: C++ : if constexpr isn't broken

#5
I had similar thoughts when looking through the slides for Andrei's talk. A main argument in favor of static if over the mechanisms C++ offers to do the same things seems to be familiarity of if statements but having scope not be respected is such an unintuitive difference for anyone with experience in curly braces languages that it seems like a net loss in clarity to me. It looks more like using macros for conditional compilation.

Overuse of if statements is generally something of a code smell and tends to make code harder to follow. To my taste the C++ way of doing things is generally preferable as a result even if more verbose. Given this is probably somewhat a matter of taste it is hard to justify changing C++ to match the D way IMO.

Re: C++ : if constexpr isn't broken

#6
post #4

I think I just got used to C++11. What is this dark magic?

If you refer to `if constexpr`: There is no dark magic. Most of it is already possible with C++11 meta programming (i.e. with std::enable_if [0]). However, it's much more readable now (and it's more fun to write :D). I think we're on a good way that compile time programming will be more easily accessible.

[0] https://en.cppreference.com/w/cpp/types/enable_if

Re: C++ : if constexpr isn't broken

#8

"a somewhat more awkward and uglier, yet slightly more correct way... all the functionality is right there. We’re not missing anything" might as well be C++'s motto :)

"The amount of things you have to know in C++ is arguably a bit larger."

Arguably, lol.

Re: C++ : if constexpr isn't broken

#9
I agree with this blog writer. Allowing typedefs to cross static if boundaries would be a major change to the way I read code. Unless there's a more substantive example demonstrating why using the existing metaprogramming is insufficient, its benefits are imo outweighed by its cost.

Re: C++ : if constexpr isn't broken

#10
post #7

Why is it every time I see a new article about C++ features my eyes glaze over? Am I just getting old, or has C++ jumped the shark?

I had a similar feeling. As a long-time C# programmer (how old is C#? That many years), I look at this and get really wide-eyed, "how is that at all a good idea?!"

A lot of these features seem like ways to hack around the fact that C++ templates are not generics, they are literally templates for writing copies of classes. It seems like features like this are going to make code size explode.

A lot of the examples also seem very smelly from an OOP perspective. We should one class be able to have different fields depending on template parameters? That seems like something one should do in a subclass.

Post reply on HN