简体   繁体   English

如何防范C ++ 03和C ++ 11的构造函数?

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

This is similar to What differences, if any, between C++03 and C++11 can be detected at run-time? 这类似于在运行时可以检测到C ++ 03和C ++ 11之间的差异(如果有的话)? . 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? 当在C ++ 03和C ++ 11中使用源时,我们应该如何保护移动构造函数 (以及移动赋值 )?

Is the following sufficient (is move semantics something all C++ compilers adopted due to it being essential/core feature)? 以下是否足够( 移动语义是所有C ++编译器都采用的东西,因为它是必不可少的/核心功能)?

#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 ? 如果我们需要编译器特定的宏,那么我们如何处理Visual Studio 2012 __cplusplus和C ++ 11等情况呢?

Sorry to ask. 很抱歉问。 I don't have some of these compilers to test on, like Visual Studio 2012, Intel ICC and Comeau. 我没有一些这样的编译器可以测试,比如Visual Studio 2012,Intel ICC和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. 编辑 :该库使用GNUmake文件和标准C ++ 03.它不使用Autotools,它不使用Cmake,它不使用Boost。

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: 移动语义是C ++ 11核心新功能之一(它是新标准在某些方面的原因之一),因此对于任何符合标准的编译器,它应该足以满足:

#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. 当然,一旦你进入编译器细节的领域,比如破坏的编译器,或者如果你想移动语义在提供“C ++ 0x”或仿真模式而不是真实的编译器中工作,那么......好吧,你进入编译器细节的土地。 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 . 对于那些你不需要甚至不需要采用任何外部库(无论是Boost,cxxomfort等)的子集,只需复制和调整Predef Wiki中的相关宏和测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM