gpt4 book ai didi

c++ - std::ignore 与结构化绑定(bind)?

转载 作者:行者123 更新时间:2023-12-02 10:36:31 25 4
gpt4 key购买 nike

序幕:

std::tuple<int, int, int> f();
std::tuple<int, int, float, int> g();

C++1z 将引入结构化绑定(bind)的语法,这将使编写而不是
int a, b, c;
std::tie(a, b, c) = f();

就像是
auto [a, b, c] = f();

但是, std::tie还允许指定 std::ignore忽略某些组件,例如:
std::tie(a, b, std::ignore, c) = g();

是否可以使用新的结构化绑定(bind)语法做类似的事情?它将如何运作?

最佳答案

结构化绑定(bind)提案包含一个专门的部分来回答您的问题 (P0144R2):

3.8 Should there be a way to explicitly ignore components?

The motivation would be to silence compiler warnings about unused names. We think the answer should be “not yet.” This is not motivated by use cases (silencing compiler warnings is a motivation, but it is not a use case per se), and is best left until we can revisit this in the context of a more general pattern matching proposal where this should fall out as a special case.

Symmetry with std::tie would suggest using something like a std::ignore:

tuple<T1,T2,T3> f();

auto [x, std::ignore, z] = f(); // NOT proposed: ignore second element

However, this feels awkward.

Anticipating pattern matching in the language could suggest a wildcard like _ or *, but since we do not yet have pattern matching it is premature to pick a syntax that we know will be compatible. This is a pure extension that can wait to be considered with pattern matching.



但是,请注意,该标准的工作草案目前正在由相关的国家机构 (NB) 进行修订,并且有一条 NB 评论要求此功能( P0488R0,US100):

Decomposition declarations should provide syntax to discard some of the returned values, just as std::tie uses std::ignore.

关于c++ - std::ignore 与结构化绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60033201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com