I want to make a header for both C++0x/11 and non C++0x/11 so that it has backwards compatibility:
class Foo{
public:
Foo(){}
Foo(Foo&){}
#ifdef CXX0X //How to make it work here?
Foo(Foo&&){}
#endif
};
I want to make a header for both C++0x/11 and non C++0x/11 so that it has backwards compatibility:
class Foo{
public:
Foo(){}
Foo(Foo&){}
#ifdef CXX0X //How to make it work here?
Foo(Foo&&){}
#endif
};
I would take a look at the boost config library. It's intended for library writers and has a lot of macros to deal with C++ compiler versions and implementation details. Even if you know it's a C++11 compiler you may still need to know some conformance details - boost config will tell you. Here's a link to the docs:
http://www.boost.org/doc/libs/1_55_0/libs/config/doc/html/index.html
I would recommend against checking the __cplusplus
as neither MSVC or GCC have hiked it up to 201103L
yet and both have had support for rvalue references for a few years.
You could count on compiler-specific macros to do what you would like:
_MSC_VER >= 1600
__GXX_EXPERIMENTAL_CXX0X__
__has_feature(cxx_rvalue_references)
There are a couple of drawbacks to doing it this way, though. Since the macros are compiler specific and the goal is to support more than one compiler, they would need to be wrapped in another macro. Also, there is talk of __GXX_EXPERIMENTAL_CXX0X__
being removed eventually.
So, it would be better to use a prebuilt tool like Boost.Config. It provides this functionality already with the macro BOOST_NO_RVALUE_REFERENCES
.
Your example would read something like:
#include <boost/config.hpp>
class Foo{
public:
Foo();
Foo(Foo&);
#ifndef BOOST_NO_RVALUE_REFERENCES
Foo(Foo&&);
#endif
};
Remember that the definition of the function would have to be wrapped similarly.