简体   繁体   English

如何在C ++中模拟解构?

[英]How can I emulate destructuring in C++?

In JavaScript ES6, there is a language feature known as destructuring . 在JavaScript ES6中,有一种称为解构的语言功能。 It exists across many other languages as well. 它也存在于许多其他语言中。

In JavaScript ES6, it looks like this: 在JavaScript ES6中,它看起来像这样:

var animal = {
    species: 'dog',
    weight: 23,
    sound: 'woof'
}

//Destructuring
var {species, sound} = animal

//The dog says woof!
console.log('The ' + species + ' says ' + sound + '!')

What can I do in C++ to get a similar syntax and emulate this kind of functionality? 我可以在C ++中做些什么来获得类似的语法并模拟这种功能?

For the specific case of std::tuple (or std::pair ) objects, C++ offers the std::tie function which looks similar: 对于std::tuple (或std::pair )对象的特定情况,C ++提供了类似的std::tie函数:

std::tuple<int, bool, double> 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. 我完全不像你提出的那样,没有意识到符号的方法。

In C++17 this is called structured bindings , which allows for the following: 在C ++ 17中,这称为结构化绑定 ,它允许以下内容:

struct animal {
    std::string species;
    int weight;
    std::string sound;
};

int main()
{
  auto pluto = animal { "dog", 23, "woof" };

  auto [ species, weight, sound ] = pluto;

  std::cout << "species=" << species << " weight=" << weight << " sound=" << sound << "\n";
}

Mostly there with std::map and std::tie : 主要是使用std::mapstd::tie

#include <iostream>
#include <tuple>
#include <map>
using namespace std;

// an abstact object consisting of key-value pairs
struct thing
{
    std::map<std::string, std::string> kv;
};


int main()
{
    thing animal;
    animal.kv["species"] = "dog";
    animal.kv["sound"] = "woof";

    auto species = std::tie(animal.kv["species"], animal.kv["sound"]);

    std::cout << "The " << std::get<0>(species) << " says " << std::get<1>(species) << '\n';

    return 0;
}

I am afraid you cannot have it the way you are used to in JavaScript (which by the way seems to be new technology in JS ). 我担心你不能像习惯JavaScript一样(顺便说一句,这似乎是JS中的新技术 )。 The reason is that in C++ you simply cannot assign to multiple variables within a structure/object/assignment expression as you did in 原因是在C ++中,你根本无法像在中那样分配给结构/对象/赋值表达式中的多个变量

var {species, sound} = animal

and then use species and sound as simple variables. 然后使用speciessound作为简单的变量。 Currently C++ simply does not have that feature. 目前C ++根本就没有这个功能。

You could assign to structures and/or objects whilst overloading their assignment operator, but I don't see a way how you could emulate that exact behaviour (as of today). 您可以在重载其赋值运算符时分配给结构和/或对象,但我没有看到如何模拟该确切行为的方法(截至今天)。 Consider the other answers offering similar solutions; 考虑提供类似解决方案的其他答案; maybe that works for your requirement. 也许这适合您的要求。

Another possibility could be done as 另一种可能性可以做到

#define DESTRUCTURE2(var1, var2, object) var1(object.var1), var2(object.var2)

which would be used like: 将使用如下:

struct Example
{
    int foo;
    int bar;
};

Example testObject;

int DESTRUCTURE2(foo, bar, testObject);

yielding local variables of foo and bar . 产生foobar局部变量。

Of course it's limited to creating variables all of the same type, although I suppose you could use auto to get around that. 当然它仅限于创建所有相同类型的变量,尽管我认为你可以使用auto来解决这个问题。

And that macro is limited to doing exactly two variables. 而那个宏仅限于做两个变量。 So you would have to create DESTRUCTURE3, DESTRUCTURE4, and so on to cover as many as you want. 因此,您必须创建DESTRUCTURE3,DESTRUCTURE4等,以覆盖您想要的任意数量。

I don't personally like the code style this ends up with, but it comes reasonably close to some of the aspects of the JavaScript feature. 我个人并不喜欢最终的代码风格,但它与JavaScript功能的某些方面相当接近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM