5 More Little Things in C++11

c_0

std::function

Function objects is a template in the c++11 library that generalizes the idea of function pointers. One advantage over function pointers is that it can refer to any callable object. You can use it to define class functions that take in functions, like this:

class someClass {
public:
    typedef std::function<void(const std::string& str, int errorNo)> callback_signature_t; 

private:
    callback_signature_t m_callback = nullptr; 
 ...
 ...

and you would define a member function of the class like this:

void set(callback_signature_t cb)
{
    m_callback = cb; 
}

Basically you can use it to pass functions with a pre-defined method signature of

void(const std::string& str, int errorNo)

in this case.

nullptr

In C++ , there was much confusion about whether to use NULL, ‘\0’ or some other variant to represent a null pointers. Well, now you have nullptr. A nullptr is a nullptr is a nullptr. Non-convert-able. No more arguments.

Assigning Value by a Pair of { } to a Container

I know, I know, almost every modern programming language has this. But still it’s a big deal in C++ when you can do :

vector<int> v {1, 2, 4, 10};

followed by

for (auto i:v)
    cout << i << endl;

How pretty is that? You can use it to declare more complicated stuff.

vector<vector<pair<int, int>>> v {{{3, 4}, {5, 6}}, {{1, 2}, {3, 4}}};

Coincidentally, when you have something like a queue like the following:

std::queue<int> q = { 4, 6, 7, 8};

clearing a queue is as easy as doing:

q = { };

If you have a struct definition:

someStruct {
    int a; 
    int b; 
    float c; 
}

You can do this:

someStruct sObj = { 4, 6, 0.0f };

I could go on and on with more and more complicated examples of using {} now to initialize containers, but you get the idea.

One Lazy Include to Rule Them All

#include <bits/stdc++.h>

A lot of the standard includes you need like are available in this header file. Saves a lot on typing and much neater in code.

Update: Wolfspaw on the reddit thread of this post mentioned that this include should only be used in rapid prototyping or competitive programming, as it increases the compile time due to the number of headers it includes. I think it should also be mentioned that this file is not part of the C++ standard, and is non-portable. Hmm… so maybe it isn’t such a good idea to use it in production code after all.  https://www.reddit.com/r/programming/comments/3tjb08/5_more_little_things_in_c11/

Non-Member begin() and end()

Previously, begin() and end() only worked with STL collection types. ie. vector. Now, you can do it with C arrays.

int anArray[100]; 
// -- invoke the sort with c array using begin() and end()
sort(begin(anArray), end(anArray)); 

Are there any other things you find useful? Let me know in the comments!

 

Advertisements

5 thoughts on “5 More Little Things in C++11

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s