CppCon2018 icon indicating copy to clipboard operation
CppCon2018 copied to clipboard

lifetime-talk of 2018 and take on structured bindings

Open igel-kun opened this issue 1 year ago • 0 comments

hey, I love your talks, but one thing had me confused: In the CppCon2018 talk about object lifetimes, the slides claim that auto [s, i] = get_Holder(); was essentially equivalent to

auto e = get_Holder();
auto& s = e.s;
auto& i = e.i;

But that would mean s is a reference, which is not how I understand auto [s, i] to behave. Indeed, std::is_reference_v<decltype(s)> says it's not a reference:

[...]

struct Holder { S s; int i; };
Holder get_Holder() { return {}; }

[...]

S get_S() {
    auto [s, i] = get_Holder(); /// structured bindings
    
    auto e = get_Holder();
    auto& _s = e.s;
    auto& _i = e.i;
    
    std::cout << std::is_reference_v<decltype(s)> << std::is_reference_v<decltype(_s)> <<"\n";
    return s;
}

Output:

01

So, I seem to not get something here. Do you have a guess on what's up?

Cheers & thanks for your time, man

igel-kun avatar May 17 '24 18:05 igel-kun