Crystallitis

C++ Superstar

May 1st, 2009 8:28 am · No Comments

This afternoon, I had the rare opportunity to listen to a well-known superstar in my realm - Dr. Bjarne Stroustrup. Bjarne Stroustrup is the father of C++. C++ is the best programming language of all time. I know this will cause some great controversy and I care not to qualify my preference, but quite simply, you should just know better. The focus of today’s talk was to introduce some of the new features of the next standard of the language - C++0x.

C++0x introduces makes available a threading support - std::threads. Threading support is long overdue in the language. The pthreads library has always been my friend, but it is preferred, particularly for cross-platform portability, for the language to support threads natively.

C++0x introduces the constexpr keyword. The constexpr will permit compile-time calculations. This will improve performance having the calculations ready for run-time. The use of constexpr is quite limited, but it does provide one more option for optimization.

C++0x will permit type inference. I thought this was rather interesting as C++ has always been a strictly typed language. Here is a code snippet that identifies a few new features of the language using less error-prone syntax:

template<class T> using Vec = vector<T,My_alloc<T>>;
Vec<double> v = { 2.3, 1.2, 6.7, 4.5  };
sort(v);
for(auto p = v.begin(); p!=v.end(); ++p)
    cout << *p << endl;

I will dissect each line starting with the first. The first line defines Vec<T> to be an alias of vector<T,My_alloc<T>>, using My_alloc instead of a default allocator. The first line also shows a change in the syntax - there is no longer a space required between >>. The second line initializes and defines an instance of the vector; using the initializer list was never permitted with user-defined containers. The third line sorts the vector, obviously. However in the C++ standard, sort must be overloaded for containers and iterators. The fourth line declares an auto pointer p with its type inferred by the initializer. The fifth line is the same as in the C++98.

There are many, many new features that I’ve not mentioned. These were just a few that Bjarne introduced, that I had remembered and had interest in. I’m interested to see how long it will take industry to use C++0x.

Tags: Bjarne Stroustrup · C++ · C++0x · constexpr · std::threads · templates

0 responses so far ↓

  • Be the first to comment below.

Leave a Comment