How to guard move constructors for C++03 and C++11?

352 views Asked by At

This is similar to What differences, if any, between C++03 and C++11 can be detected at run-time?. But in this case, I want detection to occur via the preprocessor.

How should we guard the move constructor (and move assignment) when the sources are used in both C++03 and C++11?

Is the following sufficient (is move semantics something all C++ compilers adopted due to it being essential/core feature)?

#if (__cpluplus >= 201103L)
    Foo(Foo&& other);
#endif

Or do I need to get into compiler specifics? If we need compiler specific macros, then how do we handle situations like Visual Studio 2012 __cplusplus and C++ 11?

Sorry to ask. I don't have some of these compilers to test on, like Visual Studio 2012, Intel ICC and Comeau.


EDIT: the library uses a GNUmakefile and Standard C++ 03. It does not use Autotools, it does not use Cmake, and it does not use Boost.

1

There are 1 answers

0
Luis Machuca On

Move semantics is one of the core C++11 new features (it is one of the reasons for the new Standard, in some ways) and thus for any conforming compiler it should suffice with:

#if (__cpluplus >= 201103L)
....
#endif

Ditto with, say, something as "essential" as variadics and the new semantics for auto.

Of course, once you get into the land of compiler specifics, such as a broken compiler or if you want move semantics to work in compilers that provide "C++0x" or emulation mode instead of the real thing, then... well, you get into the land of compiler specifics. For a good subset of those you don't need to even adopt any external library (be it Boost, cxxomfort, etc) but simply copy and adapt the relevant macros and tests in the Predef Wiki.