【问题标题】:Zero Initialize a Type零初始化类型
【发布时间】:2024-01-20 03:13:01
【问题描述】:

给定一个无符号整数类型的变量:foo 假设我想这样做:

const decltype<foo> bar{};

cout << (55834574890LL & ~bar) << endl;

这给了我预期的 42。但现在假设我想取消 bar 变量。所以是这样的:

cout << (55834574890LL & ~decltype<foo>{}) << endl;

但我只是得到一个错误:

错误:decltype 之前的预期主表达式

我也尝试过declval,但它返回了一个引用,这也不好。有什么办法可以做到吗?

【问题讨论】:

  • decltype&lt;foo&gt; bar{} 这个语法有效吗?你的意思可能是decltype(foo) bar{}
  • 你用的是什么编译器? const decltype&lt;foo&gt; bar{}; 不应该编译:coliru.stacked-crooked.com/a/48d19c6ef9ec5907
  • @LakshayGarg Arg 你是对的 :(
  • @NathanOliver Arg 你是对的 :(
  • 不考虑编辑原始错误是个好主意吗?当然,这只会让第一次阅读它的人感到困惑。

标签: c++ default-constructor decltype temporary declval


【解决方案1】:

你应该使用圆括号:

auto v = 55834574890LL & ~decltype(foo){};

Here's a demo.

【讨论】: