通过类比理解C ++ 17中的结构化绑定 [英] Understand structured binding in C++17 by analogy

查看:78
本文介绍了通过类比理解C ++ 17中的结构化绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解C ++ 17中引入的结构化绑定。 cppreference 的解释对我来说并不明显,但看起来像

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

cv-auto ref-operator [x, y, z] = ...

大致等于(不考虑数组大小写)

is roughly equivalent to (not to consider array case)

cv-auto ref-operator unique_name = ...
#define x unique_name.member_a
#define y unique_name.member_b
#define z unique_name.member_c

这里的关键是 xyz 不是独立定义的变量,而仅仅是返回值成员的别名。而 cv-auto ref-operator 适用于返回值,而不是别名(语法可能在这里引起误解)。例如,请参见cppreference示例

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

float x{};
char  y{};
int   z{};

std::tuple<float&,char&&,int> tpl(x,std::move(y),z);
const auto& [a,b,c] = tpl;
// a names a structured binding of type float& that refers to x
// b names a structured binding of type char&& that refers to y
// c names a structured binding of type const int that refers to the 3rd element of tpl

如果 abc 是独立定义的变量,并对其应用 const auto& ,则 c 不能为 const int 类型。

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?

推荐答案

从另一个角度考虑这一点可能很有见识。

It might be insightful to consider this from another perspective.

在C ++中,我们已经有变量,名称为 int a = 5 的对象和不是变量的对象并且没有名称: * new int 。结构化绑定是为变量的所有部分命名的方式,而整个变量没有明确的名称。因此,一起组合使用三个成员的变量 [x,y,z]

In C++ we already had variables, objects with a name int a = 5 and objects that aren't variables and do not have a name: *new int. Structured bindings are a way to have names for all parts of a variable, while the whole variable has no explicit name. So it's the combination [x,y,z] that together names an variable with three members.

重要的是,它们一起命名了一个对象,因此编译器实际上必须布局该对象。可以将独立变量独立放置在堆栈上。但是使用结构化绑定,编译器将无法执行此操作(除了普通的 as-if 规则)

Importantly, they together name an object, so the compiler actually has to layout the object. Independent variables can be placed independently on the stack. but with structured bindings the compiler cannot do so (except for the normal as-if rule)

因此,当我们考虑将 [xyz] 组合作为变量的名称,很明显 auto const& [xyz] 使组合成为 const& 变量。

So when we consider the combination [x y z] as the name of the variable, it's clear that auto const& [x y z] makes the combination a const& variable.

然后,我们必须考虑一下确切的名称分别是 x y z 意思。您的问题将其概括为:

We then have to consider what exactly the individual names x, y and z mean. Your question summarizes them as

cv-auto ref-operator unique_name = ...
#define x unique_name.member_a
#define y unique_name.member_b
#define z unique_name.member_c

有点棘手。 member_a 来自哪里?似乎唯一名称具有 member_a 。您已经排除了具有 [0] 的数组大小写。元组具有 get< 0>(tpl) get< 0> 后面可能有一个 member_a ,但 name member_a 可以是私有的。 member_a 也可能比 get< 0> 更少的const限定符。

That's a bit tricky. Where does member_a come from? It appears that unique_name has a member_a. You already excluded the array case, which has [0]. Tuples have get<0>(tpl). There might very well be a member_a behind the get<0>, but the name member_a could be private. member_a could also be less const-qualified than get<0>.

但是,在最简单的情况下,没有位域的简单 struct 确实存在相应的 member_a

But yes, for the most simple case, a simple struct without bitfields, there will indeed be a corresponding member_a.

这篇关于通过类比理解C ++ 17中的结构化绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆