Jump to content

  • Log In with Google      Sign In   
  • Create Account

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

#41 SiCrane   Moderators   -  Reputation: 9667

Like
3Likes
Like

Posted 23 October 2012 - 07:14 PM

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.

Sponsor:

#42 swiftcoder   Senior Moderators   -  Reputation: 10363

Like
4Likes
Like

Posted 23 October 2012 - 08:40 PM

Also, Sequence Points.

The fact that they qualify for the title 'little known' is a little sobering, but you'd be amazed how many people forget (or never learned of) their existence.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#43 mrbastard   Members   -  Reputation: 1573

Like
1Likes
Like

Posted 24 October 2012 - 09:32 AM

RAII

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


#44 fastcall22   Crossbones+   -  Reputation: 4456

Like
2Likes
Like

Posted 24 October 2012 - 10:04 AM

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.


Pointer-to-members are also my favorite part of the standard, though not for the same reason. I recently found myself doing this:
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 }
};

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

#45 Mussi   Crossbones+   -  Reputation: 2096

Like
1Likes
Like

Posted 24 October 2012 - 09:17 PM

I don't know how often it's used, but man do I love auto.

#46 joew   Crossbones+   -  Reputation: 3679

Like
1Likes
Like

Posted 25 October 2012 - 12:45 AM

I don't know how often it's used, but man do I love auto.

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, 25 October 2012 - 12:46 AM.


#47 SeraphLance   Members   -  Reputation: 1454

Like
3Likes
Like

Posted 25 October 2012 - 01:43 AM

any of the algorithms in <numeric> or <algorithm>

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

Edited by SeraphLance, 25 October 2012 - 01:43 AM.





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