std :: ignore与结构化绑定? [英] std::ignore with structured bindings?
问题描述
前奏:
std::tuple<int, int, int> f();
std::tuple<int, int, float, int> g();
C ++ 1z将为结构化绑定引入语法,这将使编写而不是编写成为可能
C++1z will introduce syntax for structured bindings which will make it possible to write instead of
int a, b, c;
std::tie(a, b, c) = f();
类似
auto [a, b, c] = f();
但是,std::tie
还允许指定std::ignore
来忽略某些组件,例如:
However, std::tie
also allowed to specify std::ignore
to ignore certain components, e.g:
std::tie(a, b, std::ignore, c) = g();
使用新的结构化绑定语法是否可以做类似的事情?它将如何工作?
Will it be possible to do something similar using the new structured bindings syntax? How would it work?
推荐答案
The structured bindings proposal contains a dedicated section answering your question (P0144R2):
3.8是否应该有一种方法来显式忽略组件?
动机是使有关未使用名称的编译器警告静音. 我们认为答案应该是尚未".这不是由用例引起的(使编译器警告静默是一种动机,但它本身不是用例),最好留给我们,直到我们可以在更通用的模式匹配提议的上下文中重新审视该提议时为止.作为特殊情况.
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.
与std::tie
对称将建议使用类似std::ignore
的东西:
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
但是,这感觉很尴尬.
使用该语言预期模式匹配可能会建议使用通配符,例如_
或*
,但是由于我们还没有模式匹配,因此选择一种我们知道会兼容的语法为时过早.这是一个纯粹的扩展,可以等待模式匹配来考虑.
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注释要求此功能(
However, note that the working draft of the Standard is currently being revised by the relevant National Bodies (NB), and there is a NB comment requesting this feature (P0488R0, US100):
分解声明应提供丢弃某些返回值的语法,就像
std::tie
使用std::ignore
一样.
这篇关于std :: ignore与结构化绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!