带有引用元组的结构化绑定引用

2024-02-13

The cppreference 中的结构化绑定案例2 https://en.cppreference.com/w/cpp/language/structured_binding有点难以理解。基本上,我想澄清这些情况

int x = 1;
double y = 2.0;
auto [a, b] = std::forward_as_tuple(x, y);   //a, b are both reference, why?
auto&& [c, d] = std::forward_as_tuple(x, y); //What's the difference of this and above?
auto&& [e, f] = std::tuple{x, y};  //why are NOT e, f rvalue references? Resharper shows them as value type not reference type

如果有一些函数返回引用元组,我如何使用结构化绑定来制作副本?

std::tuple<int&, double&> f;
auto [x, y] = f(); //But I want a copy from the reference, how?

std::forward_as_tuple(x, y)给你一个tuple<int&, double&>。绑定的类型是int& and double&(与绑定类型相同的方式tuple<int, double> are int and double)。基本上:

auto [a, b] = std::forward_as_tuple(x, y);
auto&& [c, d] = std::forward_as_tuple(x, y);

行为就像:

auto __e = std::forward_as_tuple(x, y);
using __E = remove_reference_t<decltype(__e)>;
tuple_element_t<0, __E>&& a = std::get<0>(std::move(__e));
tuple_element_t<1, __E>&& b = std::get<1>(std::move(__e));

auto&& __f = std::forward_as_tuple(x, y);
using __F = remove_reference_t<decltype(__f)>;
tuple_element_t<0, F>&& c = std::get<0>(std::move(__f));
tuple_element_t<1, F>&& d = std::get<1>(std::move(__f));

So a是一个右值引用int& and c是一个右值引用double&, so int& and double&分别。这个特殊的表述(我特意将其称为对参考的引用,而不是仅仅将其称为int&)是必要的,因为decltype(name) where name是一个结构化的绑定给你引用的类型,这就是为什么decltype(a)会给你int&.

The above also shows the difference between the [a, b] and the [c, d] case: the auto vs auto&& declaration applies to the unnamed object that we're destructuring. It does not affect the bindings themselves.

这个案例:

auto&& [e, f] = std::tuple{x, y};

不提供参考,因为它解压到:

auto&& __g = std::tuple{x, y};
using __G = remove_reference_t<decltype(__g)>;
tuple_element_t<0, G>&& e = std::get<0>(std::move(__g));
tuple_element_t<1, G>&& f = std::get<1>(std::move(__g));

So e是一个右值引用int, 意思是decltype(e) is int, not int&.


如果有一些函数返回引用元组,我如何使用结构化绑定来制作副本?

您无法使用结构化绑定来制作副本。结构化绑定是solely关于解构一个对象,它根本不是改变任何东西。如果您想制作副本,则必须手动执行此操作:

std::tuple<int&, double&> f = /* ... */;
std::tuple<int, double> actual_copy = f;
auto& [x, y] = actual_copy; 

In the above case, because the underlying object being destructured is an lvalue reference (auto&), this technically makes the bindings themselves lvalue references to whatever instead of rvalue references to whatever -- although I'm not sure if this is actually a meaningful distinction.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有引用元组的结构化绑定引用 的相关文章

随机推荐

  • 使用c++ opengl的贝塞尔曲线

    我使用此代码通过单击一个点来绘制贝塞尔曲线 如果我使用编写的静态公式 它会起作用drawBezier函数 它会形成正确的曲线 但是如果我使用写成的广义公式drawBezierGeneralized 最后一点有问题 我究竟做错了什么 incl
  • 将 Hadoop 中的文件获取到 Web 应用程序中

    我是 Hadoop 新手 现在我正在尝试在 eclipse 中做一个应用程序 我想在其中使用 HDFS 中存在的数据 如果我们想用Java连接数据库 我们有JDBC连接 那么 我需要做什么才能直接连接HDFS呢 在 Hadoop 中 首先
  • SQL Server 2016_无法启动镜像

    将SQL Server 2014升级到SQL Server 2016 都是企业版 后 我无法启动镜像 这在 SQL Server 2014 上正常工作 我恢复后 WITH NORECOVERY 镜像数据库 包含我通常运行的主数据库的完整备份
  • 如何强制 Iframe 在标准父框架下运行怪异

    我们有一个父页面必须在 IE9 标准模式下运行 执行 HTML5 命令 下面我们有一个必须在兼容模式 IE7 8 下运行的 iframe 据我了解 在 IE9 中 iframe 继承了父级的文档类型 那是对的吗 这个问题有什么解决办法吗 不
  • 将附加参数传递给 python 回调对象 (win32com.client.dispatchWithEvents)

    我正在使用 win32com 包与 Windows 应用程序交互 应用程序并不重要 简而言之 我想要实现的是订阅更新的表 我已经成功实现了一个回调 该回调接收表更新时返回的数据 但我现在需要的是对收到的数据采取行动 如果我可以使用附加参数实
  • VHDL - iSIM 输出未初始化,不改变状态

    您好 我是一位 Xilinx 新用户 在如何在测试台中编写激励 模拟方面遇到了麻烦 我的输出 Kd 没有给我任何合理的值 并在移动并始终保持在 1 之前的前几个时钟周期给出 u 不确定我是否写了正确的刺激 但希望有人能帮助我 我的VHDL代
  • 空字符串对于 React Link 来说是有效值吗?

    我正在写一个React js http React 20 E2 80 93 20A 20JavaScript 20library 20for 20building 20user 20interfaces 20 20https reactjs
  • 如何在Python中使用将双反斜杠替换为单反斜杠来替换字节字符串

    我想将 Python 中字节字符串的双反斜杠替换为单反斜杠 例如 有一个字节字符串 word b Z xa6 x97 x86j2 x08q r xca xe6m 我需要这个字节字符串 word b Z xa6 x97 x86j2 x08q
  • WPF 类和相应的视觉样式继承

    我已经看过 但显然在与类和样式相关时无法获得正确的语法 我有具有特定行为的控件 我派生出一些来添加额外的行为 现在 我想要一个与每个版本相对应的样式 在最简单的示例中 我将忽略这些类 因为我知道样式与视觉影响具体相关 而不是与功能影响相关
  • 添加 nuget 包源的脚本方式

    我们想要启动一个公司 nuget 包存储库 有没有办法通过命令行添加包源 以便我们可以通过设置或其他方式配置新的包源 我们基本上不想去 工具 选项 包管理器 包源 加号按钮 添加名称和来源 在公司的每台开发人员机器上 包源存储在用户配置文件
  • 如何获取 boto3 集合的大小?

    我一直使用的方法是将Collection转换为List并查询长度 s3 boto3 resource s3 bucket s3 Bucket my bucket size len list bucket objects all 然而 这会强
  • 用颜色条调整子图

    I have made the following visualization I am at loss to figure out how to adjust the size of the third subplot according
  • 防止 $anchorScroll 修改 url

    我在用 anchorScroll https docs angularjs org api ng service 24anchorScroll滚动到 html 元素具有 ID 的页面顶部 brand
  • Spring MVC @RequestMapping 继承

    来自 Struts2 我习惯于声明 Namespace超类上的注释 或package info java 并且继承类随后将获取中的值 Namespace其祖先的注释并将其添加到操作的请求路径之前 我现在正在尝试使用 Spring MVC 做
  • 两个几乎相同的批处理脚本之一中存在语法错误:“)”无法在此处进行语法处理

    我正在尝试设置 Jenkins 服务器来自动构建 Unity 因此 我编写了两个 在我看来 基本相同的批处理脚本 这两个脚本均由 Jenkins 通过Execute Windows batch command步骤使用 Command E u
  • chrome.identity.getProfileUserInfo() 返回空 ID [重复]

    这个问题在这里已经有答案了 我不知道这是否只是开发问题 因为我还没有发布我的扩展 getProfileUserInfo 返回 email id 我是否登录 chrome 并不重要 这是我的清单中的权限 permissions activeT
  • 如何在 MVC 中获取站点的基本 url [重复]

    这个问题在这里已经有答案了 我想向用户发送一封电子邮件 他可以在其中单击链接以转移到我的网站 我不想在我的电子邮件模板中对 URL 进行硬编码 我想要这种动态 无论环境如何 它都会发送相关的 url 就像如果我在开发环境中它会发送类似的内容
  • 在 python 中创建漂亮的列输出

    我正在尝试在 python 中创建一个漂亮的列列表 以便与我创建的命令行管理工具一起使用 基本上 我想要一个类似的列表 a b c aaaaaaaaaa b c a bbbbbbbbbb c 变成 a b c aaaaaaaaaa b c
  • 在 iOS 版 Chrome 上拦截 AJAX 请求?

    我通过更改来拦截我网站中的 AJAX 请求XMLHttpRequest prototype open and send方法 这种方法在我测试的所有浏览器中都没有任何问题 然而 当涉及 iOS iPhone 版 Chrome 时 代码有一个最
  • 带有引用元组的结构化绑定引用

    The cppreference 中的结构化绑定案例2 https en cppreference com w cpp language structured binding有点难以理解 基本上 我想澄清这些情况 int x 1 doubl