Monday, January 3, 2011

cool new language features in visual c++ 10

In most of my careers, I worked in unix and linux platforms and recently I started to learn windows programming because I will join MSFT soon. I started to use Visual c++ express version and love it more and more as each day goes. No wonder Scott Meyers named visual c++ as the best tool in c++ history. No wonder a lot of programmers think c++ is visual c++.

I was impressed by some of new features introduced in vc++ 10. Those features are now only available in visual c++ (and maybe a few other compilers), but they are part of c++0x specification and should be available in other compilers soon. vc++ is just a pioneer effort among other vendors.  (Interesting enough, a lot of these new features were already implemented in Java.)

To list of some of the new features.

1. "lambda expressions support"

With "lambda expression" support, c++ programmers can now code unnamed functions/functors like Java.  When we use STL, we always need to define a lot of functors and it is always hard to manage them.  With lambda expression, programmers can always code functors and functions on the fly.  Also the code is now easier to understand. Check below sample to calculate the count of even numbers from a vector

 // use a functor
class Functor 
{
public:
       void operator() (int n) ......
private:
       int evenTotal_;

};


vector<int> vec;
Functor f;
foreach(vec.begin(), vec.end(), boost::bind (boost::ref(f), _1) );



// use lambda expression
int evenTotal =0;
foreach (vec.begin(), vec.end(), [&evenTotal](int n) 
     {
             if (n%2 ==0) evenTotal ++;
     }
);


BTW, boost has lambda expression support too.

2. Right value references and Move Constructors

I have not heard those concepts until recently I started to use vc++.  Move constructors are easy to understand. If the copy or assignment constructors need to allocate and de-allocate the memory, move constructors can be used to avoid the memory allocation and recycle. However this is only applicable when a right value is passed as the constructor parameter.  

See below example.  When we use move constructors, there is no need to allocate new memory in the copy constructor.


class MemoryBlock
{
public:
// Move copy constructor.
MemoryBlock(MemoryBlock&& other)
   : _data(NULL)
   , _length(0)
{
   std::cout << "In MemoryBlock(MemoryBlock&&). length = " 
             << other._length << ". Moving resource." << std::endl;

   // Copy the data pointer and its length from the 
   // source object.
   _data = other._data;
   _length = other._length;

   // Release the data pointer from the source object so that
   // the destructor does not free the memory multiple times.
   other._data = NULL;
   other._length = 0;
}

private:
    unsigned _length;
    char * _data;

};


When will the move constructor be used? A typical use case is in containers. In STL containers, a lot of "copy" operations are needed when adding new elements or resizing the containers, if "move" copy can be used, it will save a lot of memory allocation.


vector<MemoryBlock> vec;
vec.push_back (MemoryBlock());
vec.push_back (MemoryBlock());



String concatenation 

C++ does not have a Java StringBuffer/StringBuilder class, so the "+" String operator is actually not very efficient. Developers always use ostringstream or sprintf for string concatenation.  Now this is changed in vc++ 10 as the std::string now has the move constructors. Check below sample, there will be a temporary string object created for every "+" operator in other c++ env. In vc++ 10,  operator "+" can now append the string to another, which will decrease a lot of dynamic memory allocations.



std::string s = std::string("y") + "a" + "h" + "oo";


3. Parallel Library


This is very helpful as concurrent support is built in language itself. In my current projects, we implemented a complicated framework to run tasks in parallel using boost. What a pain......   Now we have this support from the language itself.  Details will be given later.

0 comments:

Post a Comment