Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C++11
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
16 Jan 2012CPOL 33.5K   4   5
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:

C++
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)


Written By
Technical Lead
India India
_____________________________________________________________

Did my masters from IIT-M in Advanced Manufacturing Technology and working mainly on C++ in CAD domain from 2004 onwards.
Working on web technologies using Angular 7.0 and above, HTML5, CSS3 from 2015.

Comments and Discussions

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

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.