Skip to main content

The new C++

Finally the new C++ standard is out.

Now some features a programmer coming from Java or C# to C++ was badly missing are finally there.

Some of the things I noticed that fall into that category are:

* FINALLY C++ has a foreach (i'll get back to that on an example below)
* no more posix- or quasi standard thread implementations needed: c++ now has its own threads!
* FINALLY c++ also has "final" and "override" keywords
* you can now use other constructors in a new ctor of the same class (less code redundancy)
* Delegates (which is my & Microsofts name for function pointers/function objects)


other significant changes include new semantics for non-copying (which can speed up program execution significantly) and "auto" variables, meaning that for

-------------
int x = 22;
auto a = x;
-------------

"a" will get to be an integer.

and then of course some things of the boost library now are part of the standard (like the shared_ptr).
what hurts me a bit is that networking seems still not to in the standard, but you would still need to rely on the somewhat bloated boost library for that.

there are many more new features, difficulty is always to know which ones are important. For example Lambda Expressions and also Regular Expressions are now part of C++11, but -like back when they implemented it for C#- I personally dont really see much need for it.


lets try the foreach

-------------

std::vector<std::shared_ptr<SomeClass>> aVector;

for (std::shared_ptr<SomeClass> x : aVector)
{
std::cout << x->getText() << std::endl;
}

-------------

there are several cool things about this code.
a) it shows that you have a foreach
b) did you notice the ">>" in the vector declaration? thats possible now.
c) it shows that pure c++11 syntax embraces the STL: you dont need native arrays to use foreach. STL-collections (in this case "vector") dont feel like some alien attachment to c++; you can just use them. one less argument to disapprove the use of the STL.


Conclusion
Well, it took C++ a while for to reach the 21st century.
But now at least the standard is here. The STL is still nowhere near what is implemented for Java or .NET, but it gains significant&much-needed additions and a valid question is of course how-big-a standard library (in this case called STL) you want.

The publication of the standard is of course only the first step. Just because that is released does not mean one should immediatly jump onto the train (at least for business)
Now the compiler-creators need to do their job and implement it properly. We will see how much and when the big players will implement for their compilers. Because for example having standardized threads is not really anything good if it will now be supported by the major players.
Quite some features are already implemented for gcc. The same goes for VS2010, but as it came out a while ago their implementations are in an own "tr1" namespace. The new CTP of VS2011 has many {STL} features of the new standards already implemented , but we will have to wait and see if all the implementations on gcc/msvc/intel,... are completely compatible like they should be.

Comments