Skip to main content
gamedev.net
🔒 Locked

Favorite little known or underused C++ features

Started by 3DModelerMan 45 replies 14.3k views
frob
frob
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.
hupsilardee
hupsilardee
Yeah I first learned about member function pointers when I started using wxWidgets.
operator() is pretty cool too.
patrrr
patrrr
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.
iMalc
iMalc
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.
doeme
doeme
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
Kyall
Kyall
variable arguments
...
^ is awesome
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!
Servant of the Lord
Servant of the Lord

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.
demonkoryu
demonkoryu

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...
Bregma
Bregma
Lambdas have suddenly (well, relatively speaking, given the glacial pace of standards development) made incredibly useful. I like lambdas + . The 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
Evil Steve
Evil Steve
Personally I like dropping the comma operator into functions just to upset people who have to read my code.
Geometrian
Geometrian
[font=georgia,serif]Duff's device: not exceptionally useful, but clever.[/font]
[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]
adeyblue
adeyblue
std::get_temporary_buffer & std::return_temporary_buffer.
I am convined nobody has ever used these functions for anything, ever.
Washu
Washu

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.


structures declared within an anonymous union are not legal C++. Its a visual studio extension.

-changed wording slightly
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
patrrr
patrrr

anonymous structures within an anonymous union are not legal C++. Its a visual studio extension.


Crap, I've been looking the standard up and down and I didn't find anything that would make this illegal. Though I could've looked harder. Maybe there's a difference between C11 and C++11 in this regard? But I do know that it's not just a visual studio extension; it works well on gcc and clang as well.
nox_pp
nox_pp
Variadic templates are ridiculously powerful, and antiquate a lot of redundant C++03 template metaprogramming.

Initializer lists are a much needed addition to the language. They add the ability to make classes feel more like POD's, even when they aren't.


std::vector<int> ivec = {1, 2, 3, 4};
//vs.
std::vector<int> ivec;
ivec.push_back(1);
ivec.push_back(2);
ivec.push_back(3);
ivec.push_back(4);
Washu
Washu

[quote name='Washu' timestamp='1351017811' post='4993161']
anonymous structures within an anonymous union are not legal C++. Its a visual studio extension.


Crap, I've been looking the standard up and down and I didn't find anything that would make this illegal. Though I could've looked harder. Maybe there's a difference between C11 and C++11 in this regard? But I do know that it's not just a visual studio extension; it works well on gcc and clang as well.
[/quote]


C++98 - §9.5.2
[Note: nested types and functions cannot be declared within an anonymous union.]

C++11 - §9.5.5
[ Note: Nested types and functions cannot be declared within an anonymous union. —end note ]
[/quote]
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Saruman
Saruman
Out of curiosity I'm guessing it was left out of the C++11 standard do to ambiguous naming as functions/variables would live in an unnamed inner scope?
Washu
Washu

Out of curiosity I'm guessing it was left out of the C++11 standard do to ambiguous naming as functions/variables would live in an unnamed inner scope?


In the anonymous case:


void f() {
union {
struct { int x, y; };
struct { int x, y; };
} u;
}


In the not so anonymous case its harder to come up with the same example, but there are similar issues.

I should note that the only places I've actually seen people use anonymous unions/structures was usually almost always a hack that could have been avoided.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.