Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C++11
Alternative
Tip/Trick

Usage of '_HAS_CPP0X' macro in Visual C++ compiler

Rate me:
Please Sign up or sign in to vote.
4.76/5 (7 votes)
24 Jan 2012CPOL1 min read 15.7K   4
Consider the following scenario:Your team provides libraries for use in other, unspecified applications. When designing your API, you have to consider the fact that not every customer will have access to the newest compiler.Let's say you develop a Vector class that looks like...
Consider the following scenario:

Your team provides libraries for use in other, unspecified applications. When designing your API, you have to consider the fact that not every customer will have access to the newest compiler.

Let's say you develop a Vector class that looks like this:
C++
class Vector {
private:
   size_t size_;
   double* coords_;
public:
   explicit Vector(size_t n); // construct a vector with dimension n
   Vector(const Vector& v); // copy constructor

   friend Vector add(const Vector& v1, const Vector& v2);
...
};

The implementation of the function add may look like this:
C++
Vector add(const Vector& v1, const Vector& v2) {
   Vector result(v1.size_);
   // insert check for incompatible size and exception here...
   for (size_t i = 0; i < v1.size_; ++i)
      result[i] = v1[i] + v2[1];
   return result;
}

Creating the local variable result will require a memory allocation. Returning it will then again force at least one more memory allocation, from invoking the copy constructor. Since this is a pretty basic function, you expect it to be called often, so performance matters, and memory allocations are not trivial, so you decide to take advantage of move semantics. However, you cannot know that every client using this API has access to a compiler with C++11 features, so you wrap the relevant code in a macro:
C++
class Vector {
private:
   size_t size_;
   double* coords_;
public:
   explicit Vector(size_t n); // construct a vector with dimension n
   Vector(const Vector& v); // copy constructor
#ifdef _HAS_CPP0X
   Vector(const Vector&& v); // move constructor
#endif
   ...
};

Now the return statement in the add function can use the more efficient move constructor instead of the copy constructor. Clients with VS 2010 (or who explicitely define this macro because they have another brand of compiler that also supports it) will now be able to benefit from the new semantics. Yet, clients with an older version will not complain about the new syntax.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
Graduated at TU Darmstadt in Math & CS, with a heavy focus on CAD/CAM

Programming and designing applications in C++ in the areas AI, real-time programming, client-server applications and CAD/CAM since 1985.

Personal interests: AI, computer graphics, games, reading

Comments and Discussions

 
GeneralMy vote of 1 Pin
Yoldas Askan15-Sep-14 7:01
Yoldas Askan15-Sep-14 7:01 
GeneralOh I can't vote. Accepted the alternative instead. Pin
Lakamraju Raghuram24-Jan-12 6:10
Lakamraju Raghuram24-Jan-12 6:10 
GeneralRe: I think you couldn't vote because it was still pending at th... Pin
Stefan_Lang24-Jan-12 22:00
Stefan_Lang24-Jan-12 22:00 
GeneralNice example. Actually I taught of adding an example related... Pin
Lakamraju Raghuram24-Jan-12 6:09
Lakamraju Raghuram24-Jan-12 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.