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

Tagged as

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

, 16 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Usage of '_HAS_CPP0X' macro in Visual C++ compiler
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)

Share

About the Author

Lakamraju Raghuram
Software Developer
India India
_____________________________________________________________
 
Did my masters from IIT-M in Advanced Manufacturing Technology and working mainly on C++ in CAD domain from 2004 onwards.

Comments and Discussions

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

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