How to make a header for C ++ 0x / 11 and not C ++ 0x / 11?

advertisements

This question already has an answer here:

  • How to determine the version of the C++ standard used by the compiler? 5 answers

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 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:

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.