Click here to Skip to main content
Click here to Skip to main content

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

By , 24 Jan 2012
 
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:
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:
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:
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)

About the Author

Stefan_Lang
Software Developer (Senior)
Switzerland Switzerland
Member
Graduated at TU Darmstadt in Math & CS, with a heavy focus on CAD/CAM
 
20+ years of experience programming and designing applications in C++ in the areas AI, real-time programming, client-server applications and CAD/CAM.
 
Personal interests: AI, computer graphics, games, reading

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralOh I can't vote. Accepted the alternative instead.memberLakamraju Raghuram24 Jan '12 - 6:10 
GeneralRe: I think you couldn't vote because it was still pending at th...memberStefan_Lang24 Jan '12 - 22:00 
GeneralNice example. Actually I taught of adding an example related...memberLakamraju Raghuram24 Jan '12 - 6:09 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 24 Jan 2012
Article Copyright 2012 by Stefan_Lang
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid