cplusplus / cplusplus/draft

[dcl.struct.bind] structured bindings vs. potentially conflict

Open
#5,201 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TeX
Stars
221
Forks
813
Avg merge
16h 4m
Merged PRs (30d)
36

Description

@opensdh As have discussed in this thread: https://stackoverflow.com/questions/65729849/it-seems-the-current-standard-draft-cannot-interpret-why-two-structured-binding

struct A{
    int a;
};
struct B{
    int b;
};
auto&& [x] = A{};  //#1
auto&& [x] = B{};  //#2
int main(){
}

There is no provision in the current draft that can point out the name x at #1 potentially conflicts with x at #2. In short, [basic.scope.scope] p4 is saying they correspond, [basic.link] p8 is saying they declare the same entity, obviously, their shared name denotes the same entity, and also [basic.link] p11 didn't even mention structured bindings. Finally, [basic.def.odr] also didn't mention it. Furthermore, except for this example, there is also a similar example:

struct C{
    int a; 
    int b;
};
auto [x, x] = C{0,0};

[dcl.struct.bind] p1 is saying

A structured binding declaration introduces the identifiers v0, v1, v2, ... of the identifier-list as names of structured bindings.

So, each name of the identifier denotes the corresponding structured binding introduced by a structured binding declaration. Obviously, [basic.pre] p5 is wrong in this case, which says

An entity E is denoted by the name (if any) that is introduced by a declaration of E or by a typedef-name introduced by a declaration specifying E.

I would argue that a structured binding declaration can introduce multiple structured bindings(entities). Obviously, the relationship between them is not one-to-one. Moreover, [basic.pre] p3 admits that "structured binding" itself is a distinct category of the entity. So, we should regulate which structured bindings declare the same entity or not.

@opensdh has given the patch but it seems to be not ok for the second example

This is just a missing case in [basic.link]/11: that if one (of the two declarations) declares a structured binding, the program is ill-formed.

The condition of [basic.link] is

For any two declarations of an entity E

In the second example of this issue, the entity E denoted by the two names x is introduced by the same structured binding declaration. Presumably, [basic.link] also never intends to compare any two declarations by using the same declaration as two declarations, this does not make sense.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the cited [dcl.struct.bind], [basic.scope.scope], [basic.link], [basic.def.odr], and [basic.pre] clauses alongside the two examples in the issue and the linked discussion. The issue is done when the standard wording clearly resolves both potentially conflicting structured bindings and duplicate identifiers within one declaration.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.