Wednesday, November 28, 2012

C++11: Elements of Modern C++ Style

Elements of Modern C++ Style « Sutter’s Mill:

“C++11 feels like a new language.” – Bjarne Stroustrup (creator of C++)

Example: lambda expressions
// C++98: write a naked loop (using std::find_if is impractically difficult)
vector::iterator i = v.begin(); // because we need to use i later
for( ; i != v.end(); ++i ) {
    if( *i > x && *i < y ) break;
}
// C++11: use std::find_if
auto i = find_if( begin(v), end(v), [=](int i) { return i > x && i < y; } );
Podcast: Interview on Hanselminutes by Herb Sutter


So "the world runs on C++", and it must be very good, right?

The world also runs on oil/gasoline, and while it is convenient, it is not very good.
"Hybrid" maybe a good middle ground, as in case of automobiles.

The performance benefit of C/C++ is clear, but "domain specific languages"
that efficiently translate to C/C++ are better, I think.
It would be nice to have a simple restricted syntax that translates directly to C++11,
but does not require a VM to run, to preserve performance and portability...

In fact there are examples of such tools,
for example Facebook is using PHP for write web apps,
that that code is translated to C/C++ and compiled to run efficiently.
Apparently Facebook is measuring servers efficiency in Wats/User. Twice faster program, twice less servers required. For a site with a billion of users, that is a "big deal".

No comments: