当前位置:   article > 正文

CppWeekly 06 structured binding

structured binding

这个系列是从这篇博客开始的,主要是复现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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136

上述代码的执行结果如下

Hello, StructuredBinding! 
m["v0"] = 0
m["v0"] = 1

Test 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/383336
推荐阅读
相关标签
  

闽ICP备14008679号