Sign in to follow this  

Favorite little known or underused C++ features

This topic is 1910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Yeah I first learned about member function pointers when I started using wxWidgets.
operator() is pretty cool too.

Share this post


Link to post
Share on other sites
That you can read and write from different elements in a union if the elements are structs and have the same prefix structure:

[CODE]
union {
struct {int x, y; };
struct {int u, v; };
}

x = 45;
cout << u;
[/CODE]

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. Edited by patrrr

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

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 Edited by doeme

Share this post


Link to post
Share on other sites
[quote name='frob' timestamp='1350934461' post='4992872']
RNG in the standard library is not sufficient for games.
[/quote]
Just FYI, there is a [url="http://en.wikipedia.org/wiki/C%2B%2B11#Extensible_random_number_facility"]better RNG[/url] 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.

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1351006962' post='4993109']
later I'd love to learn the proper usage of streams.
[/quote]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='Evil Steve' timestamp='1351010482' post='4993137']
Personally I like dropping the comma operator into functions just to upset people who have to read my code.
[/quote]
You are Evil Steve [img]http://public.gamedev.net//public/style_emoticons/default/ohmy.png[/img]

Share this post


Link to post
Share on other sites
[quote name='patrrr' timestamp='1350978725' post='4993018']
That you can read and write from different elements in a union if the elements are structs and have the same prefix structure:

[CODE]
union {
struct {int x, y; };
struct {int u, v; };
}

x = 45;
cout << u;
[/CODE]

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.
[/quote]

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

-changed wording slightly Edited by Washu

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1351017811' post='4993161']
anonymous structures within an anonymous union are not legal C++. Its a visual studio extension.
[/quote]

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.

Share this post


Link to post
Share on other sites
[url="http://en.wikipedia.org/wiki/Variadic_template"]Variadic templates[/url] are ridiculously powerful, and antiquate a lot of redundant C++03 template metaprogramming.

[url="http://en.wikipedia.org/wiki/C%2B%2B11#Initializer_lists"]Initializer lists[/url] 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.

[CODE]
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);
[/CODE]

Share this post


Link to post
Share on other sites
[quote name='patrrr' timestamp='1351020381' post='4993169']
[quote name='Washu' timestamp='1351017811' post='4993161']
anonymous structures within an anonymous union are not legal C++. Its a visual studio extension.
[/quote]

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]

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

[b]C++11 - §9.5.5[/b]
[ Note: Nested types and functions cannot be declared within an anonymous union. —end note ]
[/quote] Edited by Washu

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote name='joew' timestamp='1351022979' post='4993183']
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?
[/quote]

In the anonymous case:

[code]
void f() {
union {
struct { int x, y; };
struct { int x, y; };
} u;
}
[/code]

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. Edited by Washu

Share this post


Link to post
Share on other sites
[quote name='patrrr' timestamp='1351020381' post='4993169']
[quote name='Washu' timestamp='1351017811' post='4993161']
anonymous structures within an anonymous union are not legal C++. Its a visual studio extension.
[/quote]

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]
GCC does it just because Visual Studio does it, and people would complain if GCC was too incompatible with Microsoft's compiler.

If you have compiler warnings set to their proper level, GCC would tell you that it's non-standard, iirc (I ran into this a few months back). Though, the [url="http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html"]documentation[/url] for GCC does imply that C11 permits it.

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1351023222' post='4993185']
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.
[/quote]
I used to use it awhile back in a matrix class but killed it off when fixing standards issues in the codebase.

[source lang="cpp"]struct Matrix44
{
union
{
struct
{
float _m[16];
};

struct
{
float m[4][4];
};
};
};[/source] Edited by joew

Share this post


Link to post
Share on other sites
[quote name='joew' timestamp='1351026885' post='4993209']
[quote name='Washu' timestamp='1351023222' post='4993185']
I should note that the only places I've actually seen people use anonymous unions/structures was usually almost always a [b]hack that could have been avoided[/b].[/quote]
I used to use it awhile back in a matrix class but killed it off when fixing standards issues in the codebase.[/quote]
If you really need to do something like this, then an array decays to a pointer. You can always store just the 2-dimensional array and cast back to the single dimension as needed.

Though I would submit that you shouldn't be handling the data in two separate fashions here, unless you are providing some sort of abstraction over both.

Share this post


Link to post
Share on other sites
[quote name='Kyall' timestamp='1350984785' post='4993041']
variable arguments
...
^ is awesome
[/quote]

No, it's bloody not.
"Basically whenever you invoke the dread ellipses construct you leave the happy world of type safety." — SiCrane

Share this post


Link to post
Share on other sites
[quote name='ChaosEngine' timestamp='1351027797' post='4993213']
[quote name='Kyall' timestamp='1350984785' post='4993041']
variable arguments
...
^ is awesome[/quote]

No, it's bloody not.
"Basically whenever you invoke the dread ellipses construct you leave the happy world of type safety." — SiCrane[/quote]

Thus why we now need variable [b]template[/b] arguments, to fix the deficiencies previously solved via variadic arguments :)

Share this post


Link to post
Share on other sites
Sign in to follow this