3

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
};
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
  • 1
    @chris How does that distinguish between two versions of C++? – John Kugelman Feb 21 '14 at 00:40
  • @JohnKugelman `__cplusplus` has a value you can test against. See the question linked in my comment. – Cogwheel Feb 21 '14 at 00:44
  • 1
    @JohnKugelman, It's a fair start. It's one Google search away from an answer. – chris Feb 21 '14 at 00:44
  • @chris Then you should post that answer. And do it as an actual answer so folks can vote on it. Please refrain from answering questions in the comments section. Use comments to ask for more information or to suggest improvements. – John Kugelman Feb 21 '14 at 00:45
  • Moving forward will you want to know about C++14 conformance as well? – Peter R Feb 21 '14 at 00:51

2 Answers2

2

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

Peter R
  • 2,865
  • 18
  • 19
2

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.

Sean Cline
  • 6,979
  • 1
  • 37
  • 50
  • 1
    There's also the [feature test recommendation](http://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations). Clang implements it now and hopefully other compilers pick it up as well. Then you'd use `#ifdef __cpp_rvalue_references` or you can check the specific version numbers. – bames53 Feb 21 '14 at 01:08
  • @bames53, Provided implementations pick up on it, I look forward to a more standard way of testing for features. I don't, however, look forward to the macro soup it will encourage people to write. – Sean Cline Feb 21 '14 at 01:19