Giter Site home page Giter Site logo

Comments (4)

tvaneerd avatar tvaneerd commented on June 15, 2024

No, they actually are references. Or actually less than references, they are "special" references. or "synonyms":

#include <string>
#include <iostream>

struct Foo
{
   int i = 0;
   std::string s = "world";
   ~Foo() { std::cout << s; }
};

int main()
{
   auto [ i, s ] = Foo();
   std::cout << "hello ";
   s = "structured bindings";
}

program output:
hello structured bindings

I guess I should add that example.

from cpp17_in_tts.

kioba avatar kioba commented on June 15, 2024

i can see now, but first let me explain myself based on the example you have used in the post:

pair<int, string> stuff(); // <-- stuff() function header
auto [ i, s ] = stuff(); // <-- stuff() function call
use(s, ++i);

even my idea about reference is bad, because:

pair<int, string> stuff(); // <-- stuff() function header
auto& [a, b] = stuff(); // stuff() returns rvalue, which can not be referenced

maybe it could be simplified like the following:

std::tuple<int, std::string> stuff = std::make_tuple(1, "hello");
auto [i, s] = stuff;
use(s, ++i);

from cpp17_in_tts.

kioba avatar kioba commented on June 15, 2024

on the other hand if we check cppreference:Structured_binding_declaration
the example states the following:

int a[2] = {1,2};
auto f() -> int(&)[2] { return a; }
 
auto [x,y] = f(); // creates e[2], copies a into e, then x refers to e[0], y refers to e[1]
auto& [xr, yr] = f(); // xr refers to a[0], yr refers to a[1]

which means if we use auto, we get a copy of the a[2] array.

Other example like the open-std:structured bindings, which is used for the clang compiler, states the following in their example:

If E is an array type with element type T, the number of elements in the identifier-list shall be equal to the number of elements of E. Each vi is the name of an lvalue that refers to the element i-1 of the array and whose type is T; the referenced type is T. [ Note: The top-level cv-qualifiers of T are cv. -- end note ] [ Example:

      auto f() -> int(&)[2];
      auto [ x, y ] = f();   // x and y refer to elements in a copy of the array return value
      auto& [ xr, yr ] = f();  // xr and yr refer to elements in the array referred to by f's return value
    
-- end example ]

based on this you Foo example:

...
auto [ i, s ]  = { expression }; // where {expresion} is Foo()
...

each element (i and s) is direct initialized from the corresponding element of expression.

from cpp17_in_tts.

kioba avatar kioba commented on June 15, 2024

closed on structured binding update

from cpp17_in_tts.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.