Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The more I program in Go, the more retroactively irritated I get at C++. A buddy of mine did a few 'brown bag' sessions on advanced C++ templating and my jaw just dropped further and further at each one. Who has the energy for this? It's hard to write, it's hard to read, it's hard to debug.

For someone who a) knows what they're doing and b) really needs to squeeze every last millisecond out of the code I suppose they could be useful. It made me want to send Rob Pike a thank you card.



IMHO if you're messing with templates in C++ you've probably over-engineered your solution.

There are a few cases where they can make sense, like in the standard library, but most of the time without them your code will be cleaner and you'll never actually run into the case where they pay off.


I've run into cases where templates pay off once or twice. I was once able to refactor 1000+ lines of code into about 150 lines using a template class. However, I've tried more than once to learn "advanced" meta-programming techniques, and I've yet to find a use case that makes any sense.


I wrote a library a few years back to do in-place non-square transposition in the GPU register file. The algorithm had a lot of math that needed to be computed for every size matrix. I wrote it in C++03, before any constexpr, so the metaprogramming is intense. One of these days I'll rewrite it in C++20 and most of the code will go away. This code is still in use by the way.

https://github.com/bryancatanzaro/trove


If you need compile-time stuff that constexpr can't do, then you have little choice. Of course, in reality, its very rare that you "need" compile-time stuff.


It is not rare at all to want your program to handle generic output. Templates might not be the cleanest syntax but they do address a real need.


I wasn’t referring to genetics or saying that templates aren’t necessary or useful, or even that they aren’t common. Those uses of templates are relatively straightforward most of the time. Where templates, IMHO, become unwieldy is when trying to compute things at compile time. In many cases constexpr works here, but my point was that the remaining cases are relatively rare, certainly when you need it (as opposed to just wanting it, eg for performance).


> A buddy of mine did a few 'brown bag' sessions on advanced C++ templating (...) Who has the energy for this? It's hard to write, it's hard to read, it's hard to debug.

C++ programmers who dedicate themselves to template metaprogramming are like stunt performers. You see them get on their shiny bikes and flashy costumes, you clinch your fists in a mix of fear and awe when they showcase their skills, but in the end it might be entertaining but it has virtually no real practical use beyond extremely niche applications.


I'm afraid you've describe my friend to a T. Ah well, it takes all types. If we couldn't work with some strange personalities, we wouldn't make it far in this business.


C++ templates can indeed get crazy, but I miss them in every other language I use. For example, I recently wrote some code in which I needed to know whether or not a member existed in a class, at compile time. Turns out, there's a way to do that in C++ using templates and overload rules:

https://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detecto...

Yes, conceptually it's a bit nuts, but the fact that you can do stuff like this makes it unlikely that you'll ever get completely stuck trying to implement something.

I personally love C++ because it's just a giant bag of tools, even if one of those tools is a footgun.


Having experienced similar "ah thank god C++ allows me to do [ridiculous template magic]" moments, 99% of the time it is because the language is forcing you (or leading you) into some kind of design that would have never been necessary in a different language. I have seen many hours/days wasted on using esoteric C++ features in order to get around a language limitation/quirk.

So I am going out on a limb here and assuming that your needing to know whether or not a member exists in a class at compile time is simply something you would never need in a different language because the language would allow you to design for something simpler that satisfies the same requirements. As someone who writes both C++ and C# daily, I often end up comparing the two and most of the time I end up thinking "it's cool that you can do that in C++ but this would have been half the lines and cleaner code in C#".


In my case, I needed to know whether or not a member exists in order to implement object tracking. I basically have a macro that inserts a member into a class, and calls that member's constructor with the "this" pointer of the tracked class. A different function needs to know, at compile time, whether or not a class is tracked, as the way memory is allocated differs.

I didn't want to use inheritance, as tracking is disabled in a shipped build, and I want it to have as little effect on the codebase as possible. So, the simplest option seemed to be to check whether or not the member exists (and assume the name is unique enough an untracked object will not contain it). This was good enough for my purposes.

I'm not as familiar with C#, but attributes seems like a potential alternative, though I'm not sure they can be fully removed from a release build. And, of course, C# is garbage collected, which is not ideal for all types of software. If there is a better alternative, I'd be interested in hearing it though!

Don't get me wrong, I'd absolutely love a "better" C++. Rust is a great language, but I personally don't feel that the safety guarantees are worth it for every class of software. If a video game crashes, the world isn't going to end.


C++ and Go are different languages with different goals.

Why would a C++ programmer learn Go? They should learn Rust since it is pretty much a cleaned up, modernized C++.


There are a million things. Fast compile times, UTF8 strings by default, easy concurrency, great standard libraries.

And the most important thing - I can easily read and understand the code written by others.


> Why would a C++ programmer learn Go?

Perhaps to find a new reason to hate a programming language thanks to Go's GOPATH.


rust is just new, not too bloated, YET. long way to go until it can be seen as a "better C++".


Agreed, it is lacking tooling support and many other things, but it is a cleaner C++. That plus the borrow checker are its two major features. Otherwise, nobody would use it and their designers would have done a pretty bad job given the 30 years of experience they had from C++!


> It made me want to send Rob Pike a thank you card.

Do it, it'll make his day.


Maybe I should!

I sometimes feel like sending one to Bjarne Stroustrup too, because C++ is still an incredible language. I see Rob and Ken talking about how they hate C++ and I worry about poor Bjarne's feelings.


Well, features like exceptions and classes don't improve performance. You could write the same code as fast in C, even if it would perhaps be more cumbersome and error prone to read and write. You save perhaps development and maintenance cost, but not runtime cost. Features like lambdas don't improve performance either, they rather make the language easier to write and even more hard to read (think of the name of a function as part of the documentation).

In theory the higher abstraction of C++ provides for optimization opportunities for a sufficiently smart compiler. I have yet to see such though.

Write the little bits which need to be fast in C and the gross in a sane language mere mortals can read (e.g. no 'most vexing parse').




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: