gpt4 book ai didi

c++ - 结构化绑定(bind)违规

转载 作者:行者123 更新时间:2023-12-04 11:48:20 28 4
gpt4 key购买 nike

代码如下

#include <tuple>

int main()
{
auto [a] = std::make_tuple(1);
return [a]() -> int { return a; }();
}
在 clang 12 中产生错误:
<source>:6:13: error: 'a' in capture list does not name a variable
return [a]() -> int { return a; }();
<source>:6:34: error: reference to local binding 'a' declared in enclosing function 'main'
return [a]() -> int { return a; }();
使用 -std=c++20 -Wall -Wextra -pedantic-errors 来支持 Visual Studio 2019 和 gcc 11接受。 https://gcc.godbolt.org/z/jbjsnfWfj
所以它们仍然违反了结构化绑定(bind)永远不是变量名称的规则,使它们永远无法捕获?

最佳答案

So they both still violate the rule that that structured bindings are never names of variables, making them never capturable?


不,实际上是 clang 违反了标准,至少对于提供的编译器标志。
在 C++20 中,不直接支持捕获结构化绑定(bind)别名的限制 has been lifted ,允许它们直接使用而无需回退到使用 init-captures 的构造:

Change [expr.prim.lambda.capture]p8 (7.5.5.2) as follows:

If a lambda-expression explicitly captures an entity that is not odr-usable or captures a structured binding (explicitly or implicitly) , the program is ill-formed.

关于c++ - 结构化绑定(bind)违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67883701/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com