这个系列是从这篇博客开始的,主要是复现Jason Turner的“C++ Weekly With Jason Turner”视频中的代码。

024 Structured bindings

Jason在这期里简单介绍了structured binding在C++17中的运用,挺有意思。其实我在想,要是structured binding可以忽略一些变量就好了,就像是Python里的下划线“_”的作用。目前可以通过std::tiestd::ignore来实现类似的功能,但是要预先定义变量。

Structured bindings除了用在std::pairstd::tuple上之外,用在普通的array和std::array上也是可以的。

Jason同时演示了structured binding可以用在struct的成员变量上。但是当struct/class具有继承关系时,structured bindings就基本失效了。当一个class具有私有成员变量时也不行。可以看这里的讨论。

最后需要指出的是,在使用structured bindings时,注意autoauto&的差别。


#include <array>
#include <iostream>
#include <map>
#include <string>
#include <tuple>struct A {int    a = 0;float  b = 1.1f;double c = 2.2;std::string d = "d";
};class B {public:int a = 0;int b = 1.1f;
private:int c = 2.2;
};class C {public:int a = 0;int b = 1;
public:void f() {}
private:void g() {}
public:int c = 2;
};class D {public:int d = 3;
};class E0 : public C {public:int e = 4;
};class E1 : public C, D {public:int e = 4;
};class F {public:static int a;
};int F::a = 0;class G : public F {public:static int b;
};int G::b = 0;int& add_v( std::map<std::string, int>& v, const std::string& name) {if ( auto [ iter, flag ] = v.insert({name, 0}); flag ) {return iter->second;} else {throw std::runtime_error("Add failed. ");}
}int main() {std::cout << "Hello, StructuredBinding! \n";std::map<std::string, int> m;auto& ret = add_v(m, "v0");std::cout << "m[\"v0\"] = " << m["v0"] << '\n';ret = 1;std::cout << "m[\"v0\"] = " << m["v0"] << '\n';std::cout << '\n';// Test std::tie and std::ignore.bool flag = false;std::tie( std::ignore, flag ) = m.insert({"v1", 0});std::cout << "Test with plain array and std::array. \n";{int a[] = {0, 1, 2};auto [ a0, a1, a2 ] = a;std::array b = { 0, 1, 2 };auto [ b0, b1, b2 ] = b;}std::cout << "Test structured binding with structs and classes. \n";{auto [ a, b, c, d ] = A();std::cout << "a = " << a << '\n';std::cout << "b = " << b << '\n';std::cout << "c = " << c << '\n';std::cout << "d = " << d << '\n';// Decompose A into 2 elements.// auto [ aa, bb ] = A(); // This is an error.}// Test structured binding with class.{// Structured binding with privatre data members.// auto [ a, b ] = B(); // This is an error.auto [ a, b, c ] = C();C objC;auto [ ca, cb, cc ] = objC;ca = -1;std::cout << "objC.a = " << objC.a << '\n';auto & [ rca, rcb, rcc ] = objC;rca = -2;std::cout << "objC.a = " << objC.a << '\n';}// Test structured binding with class hierarchy.{// auto [ a, b, e ] = E0(); // This is an error.// auto [ a, b, d, e ] = E1(); // This is an error.// auto [ a, b ] = G(); // This is an error.}return 0;
}

上述代码的执行结果如下

Hello, StructuredBinding!
m["v0"] = 0
m["v0"] = 1Test with plain array and std::array.
Test structured binding with structs and classes.
a = 0
b = 1.1
c = 2.2
d = d
objC.a = 0
objC.a = -2

CppWeekly 06 structured binding相关推荐

  1. 矿坑系列 ── Structured binding declaration

  2. 分享一个visual assist x 2476 和2488(压缩包里有你想要的)喜欢的话,就点个赞(06.05重新分享)

    现在visual assist x 2488下载,都要填资料,有点麻烦,所以我这里下载了一个,放到云盘,有需要的同学可以下载.需要的速度下载. 补丁来源地址:https://www.cnblogs.c ...

  3. C++各种循环方式梳理及对比(2)高级循环

    0. 写在最前面 本文持续更新地址:https://haoqchen.site/2020/06/08/all-kind-of-loop-2/ 上一篇文章C++各种循环方式梳理及对比之深入到汇编看whi ...

  4. c++每调用一次函数+1_每个开发人员都应该知道的一些很棒的现代C ++功能

    c++每调用一次函数+1 As a language, C++ has evolved a lot. 作为一种语言,C ++已经发展了很多. Of course this did not happen ...

  5. 鸟哥的Linux私房菜(服务器)- 第十二章、网络参数控管者: DHCP 服务器

    第十二章.网络参数控管者: DHCP 服务器 最近更新日期:2011/07/27 想象两种情况:(1)如果你在工作单位使用的是笔记本电脑,而且常常要带着你的笔记本电脑到处跑, 那么由第四章.连上 In ...

  6. C++17中那些值得关注的特性(上)

    C++17标准在2017上半年已经讨论确定,正在形成ISO标准文档,今年晚些时候会正式发布.本文将介绍最新标准中值得开发者关注的新特新和基本用法. 总的来说C++17相比C++11的新特性来说新特性不 ...

  7. std::get(std::tuple)

    template< std::size_t I, class- Types >typename std::tuple_element<I, tuple<Types-> & ...

  8. 变量之--列表初始化和结构化绑定

    变量之(变长)列表初始化 关键词 std::initializer_list 解释 在 C++98/03 中的对象初始化方法有很多种,这无疑增大了学习难度.这中情况在C++11中终于得到解决. 先看看 ...

  9. C++17中那些值得关注的特性

    2019独角兽企业重金招聘Python工程师标准>>> C++17标准在2017上半年已经讨论确定,正在形成ISO标准文档,今年晚些时候会正式发布.本文将介绍最新标准中值得开发者关注 ...

最新文章

  1. iOS学习之路十三(动态调整UITableViewCell的高度)
  2. 郑冠杰:KDD Cup城市大脑赛题方法总结!
  3. php追加记录到文件行首的办法
  4. java 添加图片背景_java添加背景图片
  5. CF1370F2-The Hidden Pair(Hard Version)【交互题,二分】
  6. mysql select count 5万条数据很慢_mysql亿级数据数据库优化方案测试银行交易流水记录的查询...
  7. 一句话证明你是产品经理
  8. bh1750采集流程图_基于MSP430和CC2530的温室大棚数据采集系统设计
  9. iOS面试总结(待完善)
  10. SQL Server 2005 中的客户端 XML 处理
  11. Mac上使用CleanMyMac彻底擦除文件详细教程
  12. script language=javascriptwindow.location.href=http://blog.securitycn.net/script
  13. EXCEL_20211117_filter平替
  14. Zabbix自动发现和自动注册
  15. 一步教会你如何获取1688商品详情
  16. 逾期的人真有这么多吗?
  17. 从零到英雄:资产商店发行人的故事
  18. 共模电感的原理以及使用情况
  19. html5一键打包成苹果软件,GDB苹果网页一键打包工具如何使用?GDB苹果网页一键打包工具安装使用教程...
  20. Silverlight开发历程—(绘制放射渐变图形)

热门文章

  1. python绘制樱花洒落_Python画图之浪漫樱花
  2. Unique ID策略
  3. 追踪“MATLAB被禁”
  4. MyEclipse创建web项目:This kind of project is associated with the MyEclipse Java Enterprise perspective
  5. Fillchar过程全解
  6. 快速排斥、跨立实验判断线段是否相交
  7. JavaScript编写杨辉三角
  8. mysql 纯真ip 导出dat_C# 读取纯真IP数据库QQWry.dat获取地区信息
  9. 什么是驱动程序 在计算机中有何用途,D驱动器上的Drivers文件夹有什么作用?可以删除吗?...
  10. 蓝牙锁定计算机,大神教你处理win10系统创意者启用动态蓝牙锁功能的详细办法...