博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CppWeekly 06 structured binding
阅读量:2134 次
发布时间:2019-04-30

本文共 2892 字,大约阅读时间需要 9 分钟。

这个系列是从开始的,主要是复现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 
#include
#include
#include
#include
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
& 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
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"] = 0m["v0"] = 1Test with plain array and std::array. Test structured binding with structs and classes. a = 0b = 1.1c = 2.2d = dobjC.a = 0objC.a = -2

转载地址:http://diugf.baihongyu.com/

你可能感兴趣的文章
(PAT 1096) Consecutive Factors (质因子分解)
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 21.包含min函数的栈
查看>>
剑指offer 23.从上往下打印二叉树
查看>>
剑指offer 25.二叉树中和为某一值的路径
查看>>
剑指offer 34.把数组排成最小的数
查看>>
剑指offer 35.数组中只出现一次的数字
查看>>
剑指offer 58. 链表中环的入口结点
查看>>