How can I emulate destructuring in C++?

后端 未结 5 1241
轻奢々
轻奢々 2020-12-29 01:44

In JavaScript ES6, there is a language feature known as destructuring. It exists across many other languages as well.

In JavaScript ES6, it looks like this:

5条回答
  •  攒了一身酷
    2020-12-29 01:54

    For the specific case of std::tuple (or std::pair) objects, C++ offers the std::tie function which looks similar:

    std::tuple my_obj {1, false, 2.0};
    // later on...
    int x;
    bool y;
    double z;
    std::tie(x, y, z) = my_obj;
    // or, if we don't want all the contents:
    std::tie(std::ignore, y, std::ignore) = my_obj;
    

    I am not aware of an approach to the notation exactly as you present it.

提交回复
热议问题