Favorite little known or underused C++ features
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.
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.
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.
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...)
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
any of the algorithms in <numeric> or <algorithm>
Sad, but true. I cry every time I see (at least in c++)
instead of the obvious
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;
}
instead of the obvious
int sum = std::accumulate(v.begin(), v.end(), 0);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement