Favorite little known or underused C++ features
#42 Senior Moderators - Reputation: 4754
Posted 23 October 2012 - 08:40 PM
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 - SDE @ Amazon - swiftcoding [Need to sync your files via the cloud? | Need affordable web hosting?]
#44 Members - Reputation: 1875
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...)
#46 Crossbones+ - Reputation: 2422
Posted 25 October 2012 - 12:45 AM
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.I don't know how often it's used, but man do I love auto.
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 - Reputation: 422
Posted 25 October 2012 - 01:43 AM
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 obviousint sum = std::accumulate(v.begin(), v.end(), 0);
Edited by SeraphLance, 25 October 2012 - 01:43 AM.






