Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 574
fredoverflow Avatar asked Jun 27 '11 12:06

fredoverflow


People also ask

What is conditional compilation in C++?

Conditional Compilation: Conditional Compilation directives help to compile a specific portion of the program or let us skip compilation of some specific part of the program based on some conditions. In our previous article, we have discussed about two such directives 'ifdef' and 'endif'.

What is ifdef and endif in C++?

#ifdef means if defined. If the symbol following #ifdef is defined, either using #define in prior source code or using a compiler command-line argument, the text up to the enclosing #endif is included by the preprocessor and therefore compiled. #if works similarly, but it evaluates the boolean expression following it.

What are the conditional inclusions in preprocessor directive?

The preprocessor supports conditional compilation of parts of source file. This behavior is controlled by #if , #else , #elif , #ifdef , #ifndef , #elifdef , #elifndef (since C++23), and #endif directives.

What is conditional preprocessor?

A conditional is a directive that instructs the preprocessor to select whether or not to include a chunk of code in the final token stream passed to the compiler.


2 Answers

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)

like image 56
jalf Avatar answered Sep 29 '22 16:09

jalf


Boost.Config has BOOST_NO_RVALUE_REFERENCES for that.

like image 30
Georg Fritzsche Avatar answered Sep 29 '22 17:09

Georg Fritzsche