什么是结构化绑定的用例? [英] What are use cases for structured bindings?

查看:82
本文介绍了什么是结构化绑定的用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 17标准引入了新的结构化绑定功能,最初在2015年建议及其句法
。外观广泛地稍后进行了讨论

C++17 standard introduces a new structured bindings feature, which was initially proposed in 2015 and whose syntactic appearance was widely discussed later.

在阅读文档时会立即想到它们的某些用途。

Some uses for them come to mind as soon as you look through documentation.

聚集分解

我们声明一个元组:

std::tuple<int, std::string> t(42, "foo");

使用结构化绑定在一行中可以轻松获得命名的元素副本:

Named elementwise copies may be easily obtained with structured bindings in one line:

auto [i, s] = t;

等效于:

auto i = std::get<0>(t);
auto s = std::get<1>(t);

int i;
std::string s;
std::tie(i, s) = t;

元组元素的引用也可以轻松获得:

References to tuple elements can also be obtained painlessly:

auto& [ir, sr] = t;
const auto& [icr, scr] = t;

所以我们可以处理所有成员都是公共的数组或结构/类。

So we can do with arrays or structs/classes whose all members are public.

多个返回值

从函数中立即获取多个返回值的便捷方法是

A convenient way to get multiple return values from a function immediately follows from the above.

还有什么?

您能否提供其他一些,也许更少结构化绑定的明显用例?他们还能如何提高C ++代码的可读性甚至性能?

Can you provide some other, possibly less obvious use cases for structured bindings? How else can they improve readability or even performance of C++ code?

注释

正如评论中提到的那样,当前结构化绑定的实现缺少某些功能。它们是非可变的,其语法不允许显式跳过聚集成员。 此处人们可以找到有关

As it were mentioned in comments, current implementation of structured bindings lacks some features. They are non-variadic and their syntax does not allow to skip aggregate members explicitly. Here one can find a discussion about variadicity.

推荐答案


您能否提供其他一些可能不太明显的结构化用例?他们还能如何提高C ++代码的可读性甚至性能?

Can you provide some other, possibly less obvious use cases for structured bindings? How else can they improve readability or even performance of C++ code?

更多一般而言,您可以使用它来(让我说) 解包一个结构并填充其中的一组变量:

More in general, you can use it to (let me say) unpack a structure and fill a set of variables out of it:

struct S { int x = 0; int y = 1; };

int main() {
    S s{};
    auto [ x, y ] = s;
    (void)x, void(y);
}

另一种方法是:

struct S { int x = 0; int y = 1; };

int main() {
    S s{};
    auto x = s.x;
    auto y = s.y;
    (void)x, void(y);
}

对于数组也可能如此:

int main() {
    const int a[2] = { 0, 1 };
    auto [ x, y ] = a;
    (void)x, void(y);
}

无论如何,因为从a返回结构或数组也可以函数,您可能会争辩说这些示例属于您已经提到的同一组案例。

Anyway, for it works also when you return the structure or the array from a function, probably you can argue that these examples belong to the same set of cases you already mentioned.

提到的另一个好的示例在@TobiasRibizel对答案的评论中,可以遍历容器并轻松地解压缩内容。

例如,基于 std :: map :

Another good example mentioned in the comments to the answer by @TobiasRibizel is the possibility to iterate through containers and unpack easily the contents.
As an example based on std::map:

#include <map>
#include <iostream>

int main() {
    std::map<int, int> m = {{ 0, 1 }, { 2, 3 }};
    for(auto &[key, value]: m) {
        std::cout << key << ": " << value << std::endl;
    }
}

这篇关于什么是结构化绑定的用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆