在 JavaScript ES6 中,有一种称为 解构 的语言特性。它也存在于许多其他语言中。
在 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 + '!')
我可以在 C++ 中做什么来获得类似的语法并模拟这种功能?
原文由 Trevor Hickey 发布,翻译遵循 CC BY-SA 4.0 许可协议
对于
std::tuple
(或std::pair
)对象的具体情况,C++ 提供了std::tie
函数,看起来类似:我不知道与您呈现的符号完全一样的方法。