Sign in to follow this  
3DModelerMan

Favorite little known or underused C++ features

Recommended Posts

[quote name='swiftcoder' timestamp='1351027616' post='4993211']
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.
[/quote]
I have no idea why it was like that as the code was extremely old and just never touched. I believe it was due to passing to an API but that still doesn't make any sense considering the cast is a NOP so it wouldn't even save anything. Edited by joew

Share this post


Link to post
Share on other sites
[quote name='swiftcoder' timestamp='1351029275' post='4993220']
Thus why we now need variable template arguments, to fix the deficiencies previously solved via variadic arguments
[/quote]

I was so looking forward to this feature, only to find out that visual studio 2012 would not be supporting it. I really hope microsoft implements at least the really important features of C++11 soon, they're seriously running behind gcc right now. Edited by Radikalizm

Share this post


Link to post
Share on other sites
[quote name='Joe the Objective C Lover' timestamp='1351029517' post='4993225']
[quote name='swiftcoder' timestamp='1351027616' post='4993211']
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.
[/quote]
I have no idea why it was like that as the code was extremely old and just never touched. I believe it was due to passing to an API but that still doesn't make any sense considering the cast is a NOP so it wouldn't even save anything.
[/quote]
The most common cases I've seen that in is crappy game tutorials code.

Also, I like your nickname. It's much better...now that I've changed it. Edited by Washu

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1351029783' post='4993229']
Also, I like your nickname. It's much better...now that I've changed it.
[/quote]
rofl bah fix it!

Share this post


Link to post
Share on other sites
[quote name='Joe the Objective C Lover' timestamp='1351030610' post='4993238']
[quote name='Washu' timestamp='1351029783' post='4993229']
Also, I like your nickname. It's much better...now that I've changed it.
[/quote]
rofl bah fix it!
[/quote]

Personally I liked "joew" seeing as it is the *ultimate* first name plus last initial.

Share this post


Link to post
Share on other sites
[quote name='jwezorek' timestamp='1351031130' post='4993239']
[quote name='Joe the Objective C Lover' timestamp='1351030610' post='4993238']
[quote name='Washu' timestamp='1351029783' post='4993229']
Also, I like your nickname. It's much better...now that I've changed it.
[/quote]
rofl bah fix it!
[/quote]

Personally I liked "joew" seeing as it is the *ultimate* first name plus last initial.
[/quote]
Well, I was going to go for [Joe the:Objective c:Lover] but it wouldn't accept that.

Share this post


Link to post
Share on other sites
[quote name='The Woynillowicz' timestamp='1351031573' post='4993242']
Don't make me whine to Drew! [img]http://public.gamedev.net//public/style_emoticons/default/ph34r.png[/img]
[/quote]
Your wish has been granted. :D Edited by Washu

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1351031686' post='4993245']
Your wish has been granted.
[/quote]
To joew!

EDIT: not funny! Edited by joew aka The Woynillowicz

Share this post


Link to post
Share on other sites
[quote name='joew aka The Woynillowicz' timestamp='1351031714' post='4993246']
[quote name='Washu' timestamp='1351031686' post='4993245']
Your wish has been granted.
[/quote]
To joew!
[/quote]

I'm CONFUSED?!?

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1351031983' post='4993247']
I'm CONFUSED?!?
[/quote]
So am I.... this change actually introduces a compile error for some wtf reason:

[source lang="cpp"]// Original
Rectangle& operator=(const Rectangle& rhs) : left(rhs.left), bottom(rhs.bottom), right(rhs.right), top(rhs.top) {}

// Replaced with
Rectangle& operator=(const Rectangle&) = default;
[/source]

EDIT: and yeah it's just a pod struct Edited by joew

Share this post


Link to post
Share on other sites
[quote name='joew' timestamp='1351032191' post='4993249']
[quote name='Washu' timestamp='1351031983' post='4993247']
I'm CONFUSED?!?
[/quote]
So am I.... this change actually introduces a compile error for some wtf reason:

[source lang="cpp"]// Original
Rectangle& operator=(const Rectangle& rhs) : left(rhs.left), bottom(rhs.bottom), right(rhs.right), top(rhs.top) {}

// Replaced with
Rectangle& operator=(const Rectangle&) = default;
[/source]

EDIT: and yeah it's just a pod struct
[/quote]

If you're going to do one assignment operator, you should do both (move and copy).

As an additional note: VS2012 does not support default nor deleted Edited by Washu

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1351032266' post='4993250']
If you're going to do one assignment operator, you should do both (move and copy).
[/quote]
I do :)

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1351032266' post='4993250']
As an additional note: VS2012 does not support default nor deleted
[/quote]
Gross! I'll stick with clang which has the majority of the standard in [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

EDIT: FYI the issue was just that another class using Rectangle didn't have operator= defined ... that's nice it makes it easy to track down during a cleaning day like today Edited by joew

Share this post


Link to post
Share on other sites
I once used negative array indices in combination with a semi-Duff's Device - and winced in mixed horror/pride afterwards. But it was in the days when C-abuse like that used to please me (I've sinced learned).

Statements like this make me smile:
[code]if ((time = (newtime = GetTime ()) - oldtime) > 0)[/code]

Wouldn't do it in real code, but it gets a "hey! you can do this!" comment in stuff I'm fooling around with. Edited by mhagain

Share this post


Link to post
Share on other sites
My favorite hardly ever used part of C++ is pointers to members, if for no other reason than the fact that they're generally a counter example for memset() to 0 being the same as C++ assignment with 0.

Share this post


Link to post
Share on other sites
[url="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization"]RAII[/url]

No seriously. I guess that says a lot about the codebases I've worked in ...

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1351041287' post='4993301']
My favorite hardly ever used part of C++ is pointers to members, if for no other reason than the fact that they're generally a counter example for memset() to 0 being the same as C++ assignment with 0.
[/quote]

Pointer-to-members are also my favorite part of the standard, though not for the same reason. I recently found myself doing this:
[code]
template<class T>
const Point2_t<T> Rect2_t<T>::corner( int idx ) const {
assert( 0 <= idx && idx < 4 );
T Rect2_t<T>::* const (&out)[2] = corners[idx];
return Point2_t<T>( this->*out[0], this->*out[1] );
}

template<class T>
T Rect2_t<T>::* const Rect2_t<T>::corners[][2] = {
{ &Rect2_t<T>::left, &Rect2_t<T>::bottom },
{ &Rect2_t<T>::right, &Rect2_t<T>::bottom },
{ &Rect2_t<T>::right, &Rect2_t<T>::top },
{ &Rect2_t<T>::left, &Rect2_t<T>::top }
};
[/code]

It's... It's beautiful (in a sadomasochism sort of way...)

Share this post


Link to post
Share on other sites
[quote name='Mussi' timestamp='1351135075' post='4993648']
I don't know how often it's used, but man do I love auto.
[/quote]
I think we'll see auto become heavily used in the future (at least I hope!) since it makes code so much more readable especially for things like iterators in for loops, etc. Although mind you I would very rarely do that now anyway considering you can for_each with a lambda or the new for loop syntax to cycle complete containers.

My favourite things that I use heavily but I'm guessing are underused at the time:
auto
for_each / lambda
default and deleted functions
final classes
override on virtual methods
nullptr Edited by joew

Share this post


Link to post
Share on other sites
any of the algorithms in <numeric> or <algorithm>

Sad, but true. I cry every time I see (at least in c++)
[CODE]
int sum = 0;
for(int i = 0; i<v.size();i++)
{
sum+=v[i];
}
[/CODE]
instead of the obvious
[CODE]
int sum = std::accumulate(v.begin(), v.end(), 0);
[/CODE] Edited by SeraphLance

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this