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()
{
#ifdef _HAS_CPP0X
SomeFunction(nullptr); #else
SomeFunction((char*)NULL); #endif
struct M { double x; };
double pi = 3.14;
const M* m = new M();
#ifdef _HAS_CPP0X
decltype( (m->x) ) piRef = pi; #else
double& piRef = pi;
#endif
map< int, map<int,int> > _Map;
#ifdef _HAS_CPP0X
auto itr1 = _Map.cbegin(); #else
map<int, map<int,int>>::const_iterator itr1 = _Map.begin();
#endif
}
We can cook up many more examples.