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

删除回忆录丶 提交于 2019-12-19 22:00:12

问题


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:


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.



来源:https://stackoverflow.com/questions/30797388/how-to-guard-move-constructors-for-c03-and-c11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!