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

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

By , 16 Jan 2012
 
Take a scenario where we are coding in Microsoft's Visual Studio VC++ 16.0 compiler
(which supports many of the C++11 features). We want to take advantage of these new features and at the same time the code should not break with older VC++ versions (which has no C++11 support).
This tip shows a possible way of doing it.
 
==========================================
 
It's simple.
 
Starting from Visual Studio's VC++ 16.0 version compiler, we have a macro '_HAS_CPP0X', which can be utilized for this purpose. Here are a few examples:
 
void SomeFunction(int i){ } 
 
void SomeFunction(char* ch) { } 
 
void main()
{
        // Example 1
#ifdef _HAS_CPP0X
	SomeFunction(nullptr); // Take the advantage of nullptr
#else 
	SomeFunction((char*)NULL); // Hmmm...we have to go for typecasting
#endif
 
       // Example 2
         struct M { double x; };
	double pi = 3.14;
	const M* m = new M();
#ifdef _HAS_CPP0X
	decltype( (m->x) ) piRef = pi; // Let the compiler decide its type
#else
	double& piRef = pi;
#endif
 
        // Example 3
        map< int, map<int,int> > _Map;
#ifdef _HAS_CPP0X
	auto itr1 = _Map.cbegin(); // no need for typing drudgery
#else
	map<int, map<int,int>>::const_iterator itr1 = _Map.begin(); 
#endif
}
 
We can cook up many more examples.

License

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

About the Author

Lakamraju Raghuram
Software Developer Triad [ CADCAM-e ]
India India
Member
_____________________________________________________________
 
Did my masters from IIT-M in Advanced Manufacturing Technology and I am working mainly on C++ from 2004 onwards.
 
I like to play with my 16 month old daughter Pranavi [rather she plays with me]. By the way she pronounces C++ as "Chi pee-pee".
 
Hate traveling (but thanks to my wife I traveled to Hyd about 50 times in last 3 years).

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   
GeneralRe: To specify my previous comment: In example 1, the only purp...memberStefan_Lang24 Jan '12 - 5:05 
GeneralRe: While in principle I agree, I doubt a client will force you ...memberStefan_Lang24 Jan '12 - 4:03 
GeneralThere's an error in the very last line that contains actual ...memberStefan_Lang24 Jan '12 - 3:50 
GeneralIn my opinion, if you still need to use older compiler that ...memberMaximilien16 Jan '12 - 5:14 
GeneralRe: I am very much in-line with this opinion, life without pre-p...memberLakamraju Raghuram16 Jan '12 - 5:56 

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 16 Jan 2012
Article Copyright 2012 by Lakamraju Raghuram
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid