C++: Moving from C++03 to C++11

  • 0

Bjarne Stroustrup, the creator of C++, said recently that C++11 “feels like a new language — the pieces just fit together better.

Indeed, core C++11 has changed significantly.
New features include:
  • lambda expressions
  • automatic type deduction of objects
  • uniform initialization syntax 
  • delegating constructors
  • deleted and defaulted function declarations
  • nullptr
  • rvalue references
An example: suppose that v is a vector:
vector<int> v(10,10); //10 ints with value 10
and we want to call a function foo on each element of v. In C++03, we might do so this way:
for (vector::size_type i = 0; i != v.size(); ++i)
      foo(v[i]);
A more sophisticated style is to use an iterator, thereby avoiding what would otherwise be a dependency on the ability to use subscripts:
for (vector::iterator it = v.begin(); it != v.end(); ++it)
      foo(*it);
If we are to write this code fragment to take advantage of C++11, we might well do so differently. For example, we might write the first example this way:
for (decltype(v.size()) i = 0; i != v.size(); ++i)
      foo(v[i]);
but we would be more likely to write something like this:
for (auto it = v.begin(); it != v.end(); ++it)
      foo(it);
or even like this:
for (auto x: v)
      foo(x)
Looks like we moving towards interpreted languages…
Sources:

No comments:

Post a Comment