Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #425: Show the correct reference type for structured bindings. #451

Merged
merged 1 commit into from
Feb 8, 2022

Conversation

andreasfertig
Copy link
Owner

This patch changes the behavior of how the resulting reference type of a
binding is determined. Now, the code uses the holding variables
type information of a BindingDecl. This seems to be consistent with
what Clang does.

The reference kind of a binding is determined by the declaration of the
structured binding.

In [dcl.struct.bind] the standard says that the reference type for a
binding is an lvalue reference if the initializer is an lvalue and
otherwise a rvalue reference. The initializer here donates to the hidden
object, referenced as e. WE declare this implicitly by declaring the
structured binding.

We get an rvalue reference in case we declare the structured binding as
a non-reference like this:

auto [a, b, c]

We get an lvalue reference if the structured binding is declared as
this:

auto& [a, b, c]

In the rvalue case we could profit from move because the contents of the
implicitly created object can be moved out.

If we have a reference to the original object moving wouldn't be a good
ting to do, hence an lvalue reference.

std::get has overloads for both l- and rvalues and returns the
respective reference type for each overload.

This behavior is observable thanks to #381 which shows the implicit cast
of the hidden object to a rvalue.

This patch changes the behavior of how the resulting reference type of a
binding is determined. Now, the code uses the holding variables
type information of a `BindingDecl`. This seems to be consistent with
what Clang does.

The reference kind of a binding is determined by the declaration of the
structured binding.

In [dcl.struct.bind] the standard says that the reference type for a
binding is an lvalue reference if the initializer is an lvalue and
otherwise a rvalue reference. The initializer here donates to the hidden
object, referenced as e. WE declare this implicitly by declaring the
structured binding.

We get an rvalue reference in case we declare the structured binding as
a non-reference like this:
```
auto [a, b, c]
```

We get an lvalue reference if the structured binding is declared as
this:

```
auto& [a, b, c]
```

In the rvalue case we could profit from move because the contents of the
implicitly created object can be moved out.

If we have a reference to the original object moving wouldn't be a good
ting to do, hence an lvalue reference.

`std::get` has overloads for both l- and rvalues and returns the
respective reference type for each overload.

This behavior is observable thanks to #381 which shows the implicit cast
of the hidden object to a rvalue.
@codecov
Copy link

codecov bot commented Feb 8, 2022

Codecov Report

Merging #451 (f317d9a) into main (0aca0cb) will decrease coverage by 0.00%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #451      +/-   ##
==========================================
- Coverage   98.56%   98.56%   -0.01%     
==========================================
  Files          23       23              
  Lines        3135     3133       -2     
==========================================
- Hits         3090     3088       -2     
  Misses         45       45              
Flag Coverage Δ
insights-windows ∅ <ø> (∅)
ubuntu 97.92% <ø> (-0.01%) ⬇️
ubuntu-libcxx 98.02% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
CodeGenerator.cpp 98.97% <ø> (-0.01%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0aca0cb...f317d9a. Read the comment docs.

@andreasfertig andreasfertig merged commit 3fd1804 into main Feb 8, 2022
@andreasfertig andreasfertig deleted the fixIssue425 branch February 8, 2022 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant