Favorite little known or underused C++ features

Started by
45 comments, last by SeraphLance 11 years, 5 months ago
What are your favorite features of C++ or the C++ standard library? Useful features that are sort of obscure, or that you haven't seen being used very much. I think I would go with member function pointers.
Advertisement
I'd suggest the majority of the standard library is underused.
I'd further suggest that good portions of the standard library is of little use to games development.

/edit:

For underuse examples, most people will write their own simple functions rather than relying on the algorithm library. Some of the ordering and searching functions are used occasionally, but few people will use std::transform(), std::generate() when they need to come up with a bunch of items. I've seen programmers run full sorts rather than functions like stable_partition() or partial_sort() when they need just a few items. And almost nobody normally uses the heap functionality, even though it can be a very solid alternative to sorted containers, especially when it comes to bulk changes.

For not applicable functionality examples, the entire Localization library of the C++ standard library is difficult to apply to game development; it doesn't play nice with the major game development localization tools. The Diagnostics library is of limited use -- most games implement their own custom asserts, warnings, and such, and many game companies have exceptions disabled for better or worse. RNG in the standard library is not sufficient for games. Most of the IO libraries are useless for games because they are blocking. Etc.
Yeah I first learned about member function pointers when I started using wxWidgets.
operator() is pretty cool too.
That you can read and write from different elements in a union if the elements are structs and have the same prefix structure:


union {
struct {int x, y; };
struct {int u, v; };
}

x = 45;
cout << u;


I also like that the library and language is designed in such a way that the compilers can optimize usage of the library really well; gcc can optimize through almost everything in there.
I would have to say valarray.
It's potentially very relevant to games development too, yet I've never heard of its actual use anywhere.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
I personally love quite a bit of the newish C++11 functionality, which is still not used by a lot of C++ developers I know, simply because they do not know of the existence of said features and because not all the features are supported by all the compilers.
Especially std::function, auto built-in smart-pointers are some of my new friends biggrin.png

I think a lot of features are just not known to many developers and the STL contains a huge load of stuff that comes in very handy quite often, but one has to know that it is there in the first place
variable arguments
...
^ is awesome
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!

RNG in the standard library is not sufficient for games.

Just FYI, there is a better RNG in the standard library for C++11 (Also higher-accuracy cross-platform timers).
C++11, while mostly focused on improving the core language (which it did), does add a few things of interest to the standard library that will help game developers moving forward (such as easier multithreading and the TR1-promoted smart pointers).

I'm personally trying to learn the algorithms that have always been in C++, since I've not much experience in them; later I'd love to learn the proper usage of streams.

later I'd love to learn the proper usage of streams.


If you mean iostreams... i advise to skip those. They're not asynchronous (if you're using file streams), they're internally complex with many trippings for you to discover (even ignoring threading issues); you're gonna have problems with I18N (you can't use the format string as a resource, as is possible with [possibly positional] printf parameters), it's more cumbersome to read/write formatting specifications and so on...
Lambdas have suddenly (well, relatively speaking, given the glacial pace of standards development) made <algorithm> incredibly useful. I like lambdas + <algorithm>. The <random> library is a gollysend: generative environments that used to take hundreds of lines of code can be condensed down to a few dozen. std::function and std::bind working together make decoupling clear and maintainable. Uniform initialization is wonderful. Ya know, C++11 is just chock full of really, really useful stuff.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement