简体   繁体   English

移动操作的条件编译

[英]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

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 Visual Studio在VC2010中引入了支持,其内部版本为1600,因此您可以使用_MSC_VER >= 1600检查
  • GCC has supported rvalue references since version 4.3 , so you can check for that version along with __GXX_EXPERIMENTAL_CXX0X__ 版本4.3开始 ,GCC支持rvalue引用,因此您可以检查该版本以及__GXX_EXPERIMENTAL_CXX0X__
  • Clang defines __has_feature macros for doing exactly what you need: __has_feature(cxx_rvalue_references) Clang定义__has_feature 以完全满足您的需要: __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) 我也很确定Boost有一个用于此目的的宏,如果您的项目包含Boost,您可以使用它(否则您可以查看它们的实现)

Boost.ConfigBOOST_NO_RVALUE_REFERENCES

The standard method is to check the standard version : If __cplusplus==199711L then you don't have (standard) rvalue references. 标准方法是检查标准版本:如果__cplusplus==199711L那么你没有(标准)右值引用。 If __cplusplus==201103L , you do. 如果__cplusplus==201103L ,那就行了。 Obviously, this doesn't cover non-standard compilers or non-standard extensions to C++98. 显然,这不包括非标准编译器或C ++ 98的非标准扩展。

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

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