现在已经是2020年了,因此使用C++的标准要提高到C++17的方式,因为C++17提高了不少性能,在语言上改进也很多。这样需要在VC2017或VC2019里使用下面的设置来使用C++17的标准:

主要设置C++ Language Standard: ISO C++ 17

设置完成之后,就可以使用最新的C++17的特性,同时也强制地按这个标准编译了。如果是旧的代码,也许会提示一个如下的BUG:

c:\program files (x86)\microsoft sdks\windows\v7.1a\include\rpcndr.h(161): error C2872: 'byte': ambiguous symbol

这时怎么办呢?因为Windows旧的SDK定义有一个byte的类型,但在C++17里也有定义std::byte类型,这样就重复定义了。因此,需要改为只使用一个的方式,这样旧代码能编译通过,也可以写新的代码符合C++17。因而需要一个预定义一个宏:

_HAS_STD_BYTE=0

通过这样设置就可以解决问题了。

可以参考下面的文章:

We have determined that the feedback applies to another product: Windows & Windows SDK. Thank you and we value your contribution to Developer Community.

We are working with the the Windows team to get this addressed in a future update of the SDK. In the meantime, the community comments & workarounds describe the set of options available well. Thank you!

I’ll specifically highlight the following two items:

  1. Use of the _HAS_STD_BYTE=0 macro by specifying /D_HAS_STD_BYTE=0 at the compiler command line. This effectively disables std::byte support in the STL and should address scenarios where ‘byte’ is not used in code. It should be noted that this macro will eventually be removed after the Windows SDK issues are fixed. That removal can be tracked via the Microsoft/STL GitHub repo at https://github.com/microsoft/STL/issues/204.
  2. Avoid bringing std::byte into the global namespace (potential by ‘using namesapce std’) and reference the Windows SDK byte using ::byte and the C++ standard version using std::byte.

Regards,
Daniel Griffing, Visual C++ Compiler Front-End

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐