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   Moderators   

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

#42   Senior Moderators   

17824
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] [GitHub]


#43   Members   

1576
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   Moderators   

9742
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...)
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#45   GDNet+   

4002
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   Members   

4335
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   Members   

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