问题标签 [structured-bindings]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
735 浏览

c++ - structured bindings, reference ? Is it possible to remove them

Let's say I have such a tuple.

I want to do something like that :

i1 is a lvalue reference because the first value on the tuple is one lvalue reference. However, I did not write auto &[i1, i2]. So, is there a possibility to "remove" the reference in this case? So that I got i1 and i2 as "simple" int. Thanks !

0 投票
1 回答
1891 浏览

c++ - 使用结构化绑定标记为 const 的变量不是 const

我一直在编写一组类来允许一个简单的类似 python 的zip函数。以下代码段(几乎)按预期工作。但是,这两个变量ab没有const

我一直在使用 gcc 7.3.0。这是MCVE:

0 投票
2 回答
1247 浏览

c++ - 结构化绑定和 tie()

鉴于这些声明:

我可以使用结构化绑定声明来解码ab

但是,如果x1,y1等已经存在,我该怎么办?

这适用于b但不适用于a. 是否有类似的简单构造适用于,或者a我必须单独获取a[0],吗?a[1]a[2]

0 投票
1 回答
1815 浏览

c++ - 为什么结构化绑定禁用 RVO 并继续返回语句?

假设我们有一个名为AAA同时支持复制/移动的类:

在以下代码中,get_val返回second

现在second被复制,打印以下输出:

这是不幸的,因为我对结果的期望是没有调用复制构造函数,或者至少它被隐式移动了。

为了避免复制,我必须明确地应用std::move它。

然后我会收到以下结果:

我假设未执行RVO的原因是编译器可能会将second其视为参考,而get_val返回纯右值。

但是,为什么也不能期望隐式移动呢?在这种特殊情况下,在 return 语句上使用显式std::move看起来并不直观,因为您通常不希望 RVO(在大多数情况下是比 move 更好的优化)意外消失

由编译器 gcc 和 clang 测试-O3

现场演示

0 投票
1 回答
459 浏览

c++ - 结构化绑定和转发引用混合得好吗?

我知道我能做到

并且根据const的返回值的好坏something,我会为bla.

这是否也适用于结构化绑定情况,例如

我猜是这样(结构化绑定搭载在auto初始化程序上,其行为如下),但我找不到明确的肯定。

更新:初步测试似乎符合我的预期(const正确推导):

现场演示auto&&,如果正在做它的工作,会出现我所期望的错误:

我仍然想确切地知道这种行为是在哪里指定的。

注意:使用“转发引用”来表示这种行为可能会延长它,但我没有一个好名字来给出const扣除部分auto&&(或模板T&&-就此而言)。

0 投票
1 回答
1272 浏览

c++ - Understand structured binding in C++17 by analogy

I'm trying to understand structured binding introduced in C++17. The explanation on cppreference is not obvious to me, but it looks like

is roughly equivalent to (not to consider array case)

The key point here is that x y z are not independently defined variables, but just aliases of the return value members. And cv-auto ref-operator applies to the return value, not the aliases (the syntax may be misleading here). For instance, see the cppreference example

If a b c are independently defined variables, with const auto& applying to them, c cannot be of type const int.

From a practical point of view, what are the key points this analogy failed to catch?

0 投票
2 回答
713 浏览

c++ - 为什么结构化绑定是根据唯一命名的变量来定义的?

为什么通过唯一命名的变量和所有模糊的“名称绑定到”语言来定义结构化绑定?

我个人认为结构化绑定的工作方式如下。给定一个结构:

以下:

(大致)相当于

以及数组和元组的等效扩展。但显然,这太简单了,而且所有这些模糊的特殊语言都用来描述需要发生的事情。

所以我显然遗漏了一些东西,但是在折叠表达式的意义上定义明确的扩展的确切情况是什么,在标准语言中阅读起来要简单得多?

似乎结构化绑定定义的变量的所有其他行为实际上都遵循我认为将用于定义概念的好像简单扩展“规则”。

0 投票
1 回答
987 浏览

c++ - 为什么 GCC 会诊断结构化绑定的未使用变量,而 Clang 不会?

让我们从一个最小的例子开始:

使用 GCC 7.3 编译并传递-std=c++17and-Wunused-variable并运行它:

GCC 可能正确地报告了 的缺失b,但它错误地将其称为变量。引用[dcl.struct.bind]/1

结构化绑定声明将标识符列表的标识符v0、v1、v2、……作为结构化绑定的名称([basic.scope.declarative])。

所以b很明显不是一个变量,而是一个名字。使用 Clang 6.0.0 和相同的标志编译相同的代码,我们不会收到任何警告。从代码中删除return a;语句:

并用 Clang 再次编译它,我们得到:

根据我的解释,它正确地被视为[a, b]一个变量,a并且b分别作为名称。那么我的问题,考虑到变量实际上是在第一个代码的语句中使用的,为什么 GCC 会诊断出未使用的警告?breturn a;

0 投票
1 回答
504 浏览

c++ - 为什么结构化绑定依赖于 tuple_element?

结构化绑定提案的最新草案(C++17 特性基于该草案)需要std::tuple_size、成员getstd::get、和std::tuple_element以前的草稿只需要std::tuple_size和 成员getstd::get。据我所知,没有关于添加这个的讨论,它只是出现在最终草案中。tuple_element考虑到我相信它可以普遍实施,是否有令人信服的理由要求专业化

有谁知道为什么要添加这个要求?

0 投票
1 回答
1228 浏览

c++ - 结构化绑定是否可重用?

我正在使用 Windows 10、Visual Studio 2017 v15.7.1/std:c++latest /permissive-

这个带有结构化绑定的简单代码无法编译:

E1277 attributes are not allowed here

下面的代码也不会编译,同样的错误

代码

也不会编译,理所当然地抱怨重新定义。

它编译的唯一方法是

坦率地说,这是丑陋的。

这个功能是这样设计的吗?还是 VC++ 错误?