使用C++17编译时出错:error C2872: 'byte': ambiguous symbol

现在已经是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

©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付 9.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值