Search code examples
c++c++11c-preprocessormove-semanticsrvalue-reference

Conditional compilation for move operations


How can I check whether my compiler supports rvalue references or not? Is there a standard preprocessor macro, or do different compilers have different macros? Ideally, I would want to write this:

#ifdef RVALUE_REFERENCES_SUPPORTED

foobar(foobar&& that)
{
    // ...
}

#endif

Solution

  • I'm not aware of any standard preprocessor macro, but:

    • Visual Studio introduced support in VC2010, whose internal version is 1600, so you can check with _MSC_VER >= 1600
    • GCC has supported rvalue references since version 4.3, so you can check for that version along with __GXX_EXPERIMENTAL_CXX0X__
    • Clang defines __has_feature macros for doing exactly what you need: __has_feature(cxx_rvalue_references)

    So for most common compilers, it should be fairly easy to cobble something together yourself.

    I am also pretty sure that Boost has a macro for this purpose, which you may be able to use if your project includes Boost (otherwise you could look at their implementation)