Std :: ignore with structured bindings?

Prelude:

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

C ++ 1z will introduce syntax for structured bindings that will allow writing instead

 int a, b, c; std::tie(a, b, c) = f(); 

something like

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

However, std::tie also allowed to specify std::ignore to ignore some components, for example:

 std::tie(a, b, std::ignore, c) = g(); 

Is it possible to do something similar using the new structured binding syntax? How it works?

+19
c ++ language-lawyer c ++ 1z
Nov 18 '16 at 9:08
source share
2 answers

The structured binding proposal contains a highlighted section to answer your question ( P0144R2 ):

3.8 Should there be a way to explicitly ignore components?

The motivation is to disable compiler warnings about unused names. We believe that the answer should be "not yet." This is not motivated by use cases (a warning about banning the compiler is motivation, but it is not an end in itself), and it is best to leave it until we can return to it in the context of a more general sentence corresponding to a template in which it should fall out as a special case.

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

 tuple<T1,T2,T3> f(); auto [x, std::ignore, z] = f(); // NOT proposed: ignore second element 

However, this is inconvenient.

Preliminary pattern matching in the language may offer a wildcard, for example _ or * , but since we do not yet have pattern matching, it is premature to select a syntax that, as we know, will be compatible. This is a pure extension that can wait to be examined using pattern matching.

However, please note that the working draft of the standard is currently being reviewed by the relevant national authorities (NB), and there is a NB comment requesting this feature ( P0488R0 , US100):

Decomposition declarations must contain syntax to discard some return values, just as std::tie uses std::ignore .

+10
Nov 21 '16 at 6:48
source share

Is it possible to do something similar using the new structured binding syntax?

No. You just need to make a variable name that will not be mentioned later.

+2
Nov 18 '16 at 14:52
source share



All Articles