Comments (4)
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.
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.
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.
closed on structured binding update
from cpp17_in_tts.
Related Issues (11)
- Parallel execution policies HOT 1
- Indenting in C++ examples is confused. HOT 6
- `std::any` requires no calls to new? HOT 1
- Constexpr If HOT 7
- structure binding typo HOT 1
- std::optional usage example with new if is iffy HOT 3
- Byte type HOT 1
- Please consider adding a license HOT 1
- Add a bit about monostate in std::variant section?
- Mistake in first table in structured_bindings.md? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpp17_in_tts.