Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Favorite little known or underused C++ features


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
46 replies to this topic

#1 3DModelerMan   Members   -  Reputation: 1067

Like
0Likes
Like

Posted 22 October 2012 - 11:56 AM

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.

Sponsor:

#2 frob   Moderators   -  Reputation: 22693

Like
7Likes
Like

Posted 22 October 2012 - 01:34 PM

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.

Edited by frob, 22 October 2012 - 06:01 PM.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#3 hupsilardee   Members   -  Reputation: 487

Like
2Likes
Like

Posted 23 October 2012 - 01:45 AM

Yeah I first learned about member function pointers when I started using wxWidgets.
operator() is pretty cool too.

#4 patrrr   Members   -  Reputation: 1047

Like
2Likes
Like

Posted 23 October 2012 - 01:52 AM

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.

Edited by patrrr, 23 October 2012 - 01:55 AM.


#5 iMalc   Crossbones+   -  Reputation: 2314

Like
1Likes
Like

Posted 23 October 2012 - 02:11 AM

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

#6 doeme   Members   -  Reputation: 718

Like
1Likes
Like

Posted 23 October 2012 - 03:10 AM

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 Posted Image

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, 23 October 2012 - 08:02 AM.


#7 Kyall   Members   -  Reputation: 287

Like
1Likes
Like

Posted 23 October 2012 - 03:33 AM

variable arguments
...
^ is awesome
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!

#8 Servant of the Lord   Crossbones+   -  Reputation: 20974

Like
2Likes
Like

Posted 23 October 2012 - 09:42 AM

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.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#9 demonkoryu   Members   -  Reputation: 976

Like
0Likes
Like

Posted 23 October 2012 - 10:00 AM

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

#10 Bregma   Crossbones+   -  Reputation: 5394

Like
4Likes
Like

Posted 23 October 2012 - 10:24 AM

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

#11 Evil Steve   Members   -  Reputation: 1987

Like
9Likes
Like

Posted 23 October 2012 - 10:41 AM

Personally I like dropping the comma operator into functions just to upset people who have to read my code.

Steve Macpherson
Systems Programmer

Rockstar North


#12 3DModelerMan   Members   -  Reputation: 1067

Like
2Likes
Like

Posted 23 October 2012 - 10:45 AM

Personally I like dropping the comma operator into functions just to upset people who have to read my code.

You are Evil Steve Posted Image

#13 Geometrian   Crossbones+   -  Reputation: 1599

Like
2Likes
Like

Posted 23 October 2012 - 10:56 AM

Duff's device: not exceptionally useful, but clever.
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.

#14 adeyblue   Members   -  Reputation: 518

Like
1Likes
Like

Posted 23 October 2012 - 11:29 AM

std::get_temporary_buffer & std::return_temporary_buffer.
I am convined nobody has ever used these functions for anything, ever.

#15 Washu   Senior Moderators   -  Reputation: 5417

Like
0Likes
Like

Posted 23 October 2012 - 12:43 PM

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

Edited by Washu, 23 October 2012 - 02:10 PM.

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.
ScapeCode - Blog | SlimDX


#16 patrrr   Members   -  Reputation: 1047

Like
0Likes
Like

Posted 23 October 2012 - 01:26 PM

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.

#17 nox_pp   Members   -  Reputation: 490

Like
3Likes
Like

Posted 23 October 2012 - 01:30 PM

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);

Between Scylla and Charybdis: First Look <-- The game I'm working on

 

Object-Oriented Programming Sucks <-- The kind of thing I say


#18 Washu   Senior Moderators   -  Reputation: 5417

Like
3Likes
Like

Posted 23 October 2012 - 01:55 PM


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.


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 ]


Edited by Washu, 23 October 2012 - 01:55 PM.

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.
ScapeCode - Blog | SlimDX


#19 joew   Crossbones+   -  Reputation: 3679

Like
1Likes
Like

Posted 23 October 2012 - 02:09 PM

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?

#20 Washu   Senior Moderators   -  Reputation: 5417

Like
0Likes
Like

Posted 23 October 2012 - 02:13 PM

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.

Edited by Washu, 23 October 2012 - 02:15 PM.

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.
ScapeCode - Blog | SlimDX





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS