Live data from Hacker News

C++ : if constexpr isn't broken

brevzin.github.io

71–80 of 83 posts

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

#71

Earlier quoted context omitted.

> Nothing in life comes free: everything, including all abstractions, comes at some cost. Yes. As a slogan, it is imprecise. But it's always been talking about a very specific kind of cost: runtime costs. You're 100% right about there always being some kind of cost, but the slogan doesn't disagree with you. (Some prefer "zero-overhead principle" instead to make this a bit more clear.)

(1) I love your writing. (2) Even reducing it to runtime costs, it seems a bit nonsensical. Are C++ exceptions a zero cost abstraction? All the googlers I argued with about them would insist that they have unacceptably high runtime costs. OK, but templates are surely zero (runtime) cost abstractions, right? Unless you start to worry about duplicate code blowing out your instruction cache but if that's a problem, no p…

1. Thank you!

2. The other reply is right. Zero cost compared to the best possible implementation.

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

#72
post #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.

Well you already have to remember that 'if' introduces a scope but '#if' doesn't..

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

#73
post #26

Earlier quoted context omitted.

And that's exactly what's different in D, where the audience for meta-programming is "everyone". It's not just about more power, but how accessible this power is. To think you need to suffer to have this power is not true.

Nothing stops "everyone" from using the features which suit library developers.

You can't in C++ because learning the language takes 10 years and is a never-ending story.

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

#74
post #26

Earlier quoted context omitted.

And that's exactly what's different in D, where the audience for meta-programming is "everyone". It's not just about more power, but how accessible this power is. To think you need to suffer to have this power is not true.

D vs. C++ in this list of examples looked equivalently complex and equivalently power-user focused. static if not introducing a new scope seems like the sort of confusing, error-prone edge case that trips up newcomers, for example. We are taught very early on that in C-style languages curly braces means a scope. Except here in D in this particular case for some reason it's not that isn't clear why until you are very…

> Similarly operator overloading via a string that tells you what you are overloading seems... insane? Very error-prone & complex?

Frankly you should try D for just 5 minutes and see for yourself, because no it is really sane and works well. Never seen anyone complain about this...

See here it is used to implement all operators for small vectors in 46 lines: https://github.com/d-gamedev-team/gfm/blob/master/math/gfm/m...

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

#75
post #73

Earlier quoted context omitted.

Nothing stops "everyone" from using the features which suit library developers.

You can't in C++ because learning the language takes 10 years and is a never-ending story.

Any true language is ever evolving, even spoken languages.

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

#76
post #57
post #39

Earlier quoted context omitted.

There is nothing as "a proper macro". There is proper meta-programming support or a badly design language.

A macro is a metal-level abstraction. So proper macros require proper metaprogramming support. And, I would argue, the other way around.

And I would argue that a Macro is external language put on top of your language as a patch (hack), when your language is not expressive enough for genericity.

Then yes, there is nothing as "proper" Macro. There is just an attempt to fix a limited language in the first place.

And it is something that C++ arrived to solve ( not that badly ) currently.

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

#77

Earlier quoted context omitted.

(1) I love your writing. (2) Even reducing it to runtime costs, it seems a bit nonsensical. Are C++ exceptions a zero cost abstraction? All the googlers I argued with about them would insist that they have unacceptably high runtime costs. OK, but templates are surely zero (runtime) cost abstractions, right? Unless you start to worry about duplicate code blowing out your instruction cache but if that's a problem, no p…

Zero cost abstraction is not the same thing as a free lunch. It's a goal that using the abstraction will have no runtime cost relative to implementing the same or equivalent functionality manually. It goes hand in hand in C++ with the "don't pay for what you don't use" principle (again talking about runtime performance cost). When it comes to exceptions, it's generally true on x64 that you don't pay for what you don'…

> When it comes to exceptions, it's generally true on x64 that you don't pay for what you don't use (there's no performance penalty to exception handling if you don't throw) although that hasn't always been true for all platforms and implementations. It's also generally true that you couldn't implement that kind of non local flow control more efficiently yourself, although the value of that guarantee is a little questionable with exception handling.

I'm inclined to agree with you but just about everyone at Google says the opposite and most C++ shops I've seen agree with them. I've made this argument and lost repeatedly. So, it seems like the community can't even agree on which abstractions are zero cost (or maybe whether some zero cost abstractions are actually zero cost?). To the extent that the community itself has no consensus about these things, maybe they're not a marketing slogan that's helpful to use.

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

#78
post #74

Earlier quoted context omitted.

D vs. C++ in this list of examples looked equivalently complex and equivalently power-user focused. static if not introducing a new scope seems like the sort of confusing, error-prone edge case that trips up newcomers, for example. We are taught very early on that in C-style languages curly braces means a scope. Except here in D in this particular case for some reason it's not that isn't clear why until you are very…

> Similarly operator overloading via a string that tells you what you are overloading seems... insane? Very error-prone & complex? Frankly you should try D for just 5 minutes and see for yourself, because no it is really sane and works well. Never seen anyone complain about this... See here it is used to implement all operators for small vectors in 46 lines: https://github.com/d-gamedev-team/gfm/blob/master/math/gfm/…

It appears to only be primarily useful if you are writing a pure wrapper class where you proceed to delegate to an actual implementation.

But you could still do that and not be string-based. It could (and should!) be an enum of the operator instead. opBinary takes a fixed number of ops, but the parameter type of string has an infinite number of values.

Whether or not the design of having a single operator overload method is a good idea or not is independent from what I'm specifically calling insane which is that the parameter type to that method is a string.

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

#79
post #74

Earlier quoted context omitted.

> Similarly operator overloading via a string that tells you what you are overloading seems... insane? Very error-prone & complex? Frankly you should try D for just 5 minutes and see for yourself, because no it is really sane and works well. Never seen anyone complain about this... See here it is used to implement all operators for small vectors in 46 lines: https://github.com/d-gamedev-team/gfm/blob/master/math/gfm/…

It appears to only be primarily useful if you are writing a pure wrapper class where you proceed to delegate to an actual implementation. But you could still do that and not be string-based. It could (and should!) be an enum of the operator instead. opBinary takes a fixed number of ops, but the parameter type of string has an infinite number of values. Whether or not the design of having a single operator overload me…

If it is string-based meta-programming you find ugly (as many do as first), consider the alternatives are maybe not much better in practice.

https://forum.dlang.org/post/l5srs7$2m3$1@digitalmars.com

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

#80
post #73

Earlier quoted context omitted.

You can't in C++ because learning the language takes 10 years and is a never-ending story.

Any true language is ever evolving, even spoken languages.

Not all languages are the same, C++ is vastly more complex than many other programming languages (arguably all of them). The only reason to keep learning C++ after years of practice is the sunken cost.
Post reply on HN