11

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
fredoverflow
  • 256,549
  • 94
  • 388
  • 662

3 Answers3

15

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)

jalf
  • 243,077
  • 51
  • 345
  • 550
  • 4
    I must admit I really love the way Clang chose to advertise its various features and extensions, it's so much clearer than checking with a version number, and much more detailed too. – Matthieu M. Jun 27 '11 at 14:22
9

Boost.Config has BOOST_NO_RVALUE_REFERENCES for that.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
5

The standard method is to check the standard version : If __cplusplus==199711L then you don't have (standard) rvalue references. If __cplusplus==201103L, you do. Obviously, this doesn't cover non-standard compilers or non-standard extensions to C++98.

MSalters
  • 173,980
  • 10
  • 155
  • 350