Old fashioned S010 (0x1392) wrote,
Old fashioned S010


C++11 is a major upgrade of the language and it's standard library. C++ is now much closer to the so called "productivity languages", yet as efficient as ever.

I think with these improvements C++ becomes a "good enough" language to be the default choice. It's now much more productive, it's very efficient, it's well supported on all major platforms, free compilers are readily available and there are tons and tons of useful free libraries out there.
This will be especially true with the introduction of Windows Runtime in Windows 8.

Here are four most important, in my opinion, new features which will change the way C++ code is written regardless of the problem domain and regardless of your favourite subset of the language.

New meaning of the auto keyword

When declaring a variable, you can now use auto keyword in place of the type: the type will be automatically deduced from initializer.

/* C++98 */ map<string, string>::iterator it = m.begin(); /* C++11 */ auto it = begin(m);

Ranged for

Ranged for allows to iterate through any container that overloads begin() and end() functions. These functions are overloaded for C-style basic type arrays by standard library.

int a[] = { 1, 2, 3, 4, 5 }; /* C++98 */ for (int *p = a; p < a + sizeof(a)/sizeof(*a); ++p) *p *= 2; /* C++11 */ for (auto &i : a) i *= 2; for (auto s : { "one", "two", "three" }) cout << s << endl;

Intializer lists

Initializer lists bring uniform initialization of objects. You can now initialize just about anything, including basic types like int, using the curly-brace syntax.

/* C++98 */ pair<string, int> pairs[] = { make_pair("one", 1), make_pair("two", 2), }; map<string, int> m(pairs, pairs + sizeof(pairs)/sizeof(*pairs)); /* C++11 */ map<string, int> m { { "one", 1 }, { "two", 2 }, };


Lambdas are a syntax for creating function_objects/anonymous_functions/closures. In other words, little pieces of code which you can pass around.</br> I think the introduction of lambdas increases the usefulness of the standard library <algorithm> header by a ten-fold.</br> The following example isn't great but it shows how things are now much more flexible.

/* C++98 */ class Is_longer { public: Is_longer(string::size_type thresh): thresh(thresh) { } bool operator() (const string &s) const { return s.size() > this->thresh; } private: string::size_type thresh; }; vector<string> get_longer_strings(const vector<string> &v, string::size_type thresh) { vector<string> strs; copy_if(v.begin(), v.end(), inserter(strs, strs.begin()), Is_longer(thresh)); return strs; } /* C++11 */ vector<string> get_longer_strings(const vector<string> &v, string::size_type thresh) { vector<string> strs; for_each(begin(v), end(v), [&](const string &s) { if (s.size() > thresh) strs.push_back(s); }); return strs; }

Bjarne Stroustrup's C++11 FAQ
Herb Sutter: Elements of Modern C++ Style
C++11 Reference

Tags: c++, programming
  • Post a new comment


    Comments allowed for friends only

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded