Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

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

, 24 Jan 2012
Rate this:
Please Sign up or sign in to vote.
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:
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)

Share

About the Author

Stefan_Lang
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

 
GeneralOh I can't vote. Accepted the alternative instead. PinmemberLakamraju Raghuram24-Jan-12 6:10 
Oh I can't vote. Accepted the alternative instead.
GeneralRe: I think you couldn't vote because it was still pending at th... PinmemberStefan_Lang24-Jan-12 22:00 
GeneralNice example. Actually I taught of adding an example related... PinmemberLakamraju Raghuram24-Jan-12 6:09 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 24 Jan 2012
Article Copyright 2012 by Stefan_Lang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid