IS是`auto ua = unsigned int {};`合法c++?

发布于 2025-02-09 21:14:34 字数 364 浏览 2 评论 0原文

此代码使用MSVC编译,但不使用GCC或Clang编译。

    auto a = int{};
    auto ua = unsigned int {};

参见 Compiler Explorer上的演示

我强烈怀疑它可能是合法的C ++,但是古代之间的混合物” C样式 /带有空间的类型”以及在C ++中进行初始化的50种不同方法使这对编译器来说是非常困难的工作。

This code compiles with MSVC, but not with GCC or Clang.

    auto a = int{};
    auto ua = unsigned int {};

See demo on compiler explorer

I strongly suspect it might be legit C++, but that the mix between the ancient "C style / types with spaces" and the 50 differents ways of doing initialization in C++ make this a very hard job for compilers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2025-02-16 21:14:34

根据C ++ 20标准(7.6.1.4显式转换(功能符号)):

1 a 简单型特性(9.2.9.3)或 typename-specifier (13.8),然后是括号内的可选表达列表,或者是支撑 - 清单(初始器)构造给定初始化器的指定类型的值。如果类型是推导类型类型的占位符,则将其替换为本次级别的其余部分的Overload分辨率选择的函数的返回类型。

所以你需要写

auto ua = unsigned {};

According to the C++ 20 Standard (7.6.1.4 Explicit type conversion (functional notation)):

1 A simple-type-specifier (9.2.9.3) or typename-specifier (13.8) followed by a parenthesized optional expression-list or by a braced-init-list (the initializer) constructs a value of the specified type given the initializer. If the type is a placeholder for a deduced class type, it is replaced by the return type of the function selected by overload resolution for class template deduction (12.4.2.9) for the remainder of this subclause.

So you need to write

auto ua = unsigned {};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文