'_HAS_CXX17' marco 是否可用于自定义项目标头以启用 C++17 语言集功能? [英] Is '_HAS_CXX17' marco usable in custom project headers to enable C++17 language set features?

查看:38
本文介绍了'_HAS_CXX17' marco 是否可用于自定义项目标头以启用 C++17 语言集功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建使用标准 C++ 中的可选"的头文件.但是,我的标头将从 Visual Studio 2015 和 Visual Studio 2017 项目中引用.

我想要一些东西,例如对于 Visual Studio 2017(带有 C++ 17 lang 功能集),使用 std::optional 并且在 Visual Studio 2015 中使用 boost::optional.

我正在考虑这样的事情:

#include #if _HAS_CXX17#include <可选>模板 使用 Optional = std::optional;#别的#include "boost/optional/optional.hpp"模板 使用 Optional = boost::optional;#万一

这样使用'_HAS_CXX17'宏可以吗?有没有更好的方法来做到这一点?

解决方案

简短的回答是:在 Visual C++ 运行时的实现中依赖内部预处理器定义是不安全的,并且从技术上讲所有以单个 _ 开头的编译器符号都保留供实现使用.

例如,_NOEXCEPT 在 Visual Studio 2015 和 2017 内部已经使用,但从 VS 2017(15.8 更新)开始,这个宏不再存在;标题直接使用 noexcept.

建议使用 __has_include 很好,但在 VS 2017(15.3 更新)之前不受支持.

另一个挑战是 __cplusplus 并不表示您正在使用 /std:c++17 除非您使用 VS 2017(15.7 更新)和新的/Zc:__cplusplus 默认关闭的开关.

在一系列 VS 版本中执行此操作的最安全方法可能是:

#if (__cplusplus >= 201703L) ||(定义(_MSVC_LANG) && (_MSVC_LANG >= 201703L) && (_MSC_VER >= 1913))#if __has_include(<可选>)#include <可选>模板 使用 Optional = std::optional;#别的#include "boost/optional/optional.hpp"模板 使用 Optional = boost::optional;#万一#别的#include "boost/optional/optional.hpp"模板 使用 Optional = boost::optional;#万一

<块引用>

请参阅 Visual C++ 语言一致性>

I want to create headers which use 'optional' from standard C++. However, My headers will be referred from Visual Studio 2015 as well as Visual Studio 2017 projects.

I would like to have something, such that for Visual Studio 2017 ( with C++ 17 lang feature set) , std::optional is used and with Visual Studio 2015, boost::optional gets used.

I am thinking of something like this:

#include <yvals.h>
#if _HAS_CXX17
 #include <optional>
 template <typename T> using Optional = std::optional<T>;
#else
 #include "boost/optional/optional.hpp"
 template <typename T> using Optional = boost::optional<T>;
#endif

Is it okay to use '_HAS_CXX17' macro this way? Is there a better way of doing this?

解决方案

The short answer is: No It's not safe to rely on internal preprocessor defines in the implementation of the Visual C++ Runtime, and technically all compiler symbols that begin with a single _ are reserved for use by the implementation.

For example, _NOEXCEPT has been used internally in Visual Studio 2015 and 2017, but as of VS 2017 (15.8 update), this macro no longer exists; the headers just use noexcept directly.

The recommendation to use __has_include is good, but isn't supported prior to VS 2017 (15.3 update).

The other challenge is that __cplusplus doesn't indicate you are using /std:c++17 unless you are using VS 2017 (15.7 update) with the new /Zc:__cplusplus switch which is off by default.

Probably the safest way to do this across a range of VS versions would be:

#if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L) && (_MSC_VER >= 1913))
#if __has_include(<optional>)
 #include <optional>
 template <typename T> using Optional = std::optional<T>;
#else
 #include "boost/optional/optional.hpp"
 template <typename T> using Optional = boost::optional<T>;
#endif
#else
 #include "boost/optional/optional.hpp"
 template <typename T> using Optional = boost::optional<T>;
#endif

See Visual C++ Language Conformance

这篇关于'_HAS_CXX17' marco 是否可用于自定义项目标头以启用 C++17 语言集功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆