tuple a = {1, 3.7}; //Creates a tuple of type tuple<int, double> with values 1 and 3.7
tuple b = [1, 3.7]; //Ditto
[double, double] GetAngleVector(double x)
{
return [cos(x), sin(x)];
}
//All of these would be 100% valid:
tuple t = GetAngleVector(2.7);
pair p = GetAngleVector(2.7);
array<double, 2> arr = GetAngleVector(2.7);
auto [x,y] = GetAngleVector(2.7);
struct Point2D {
double x,y;
};
Point2D point = GetAngleVector(2.7);
tuple<double, double> GetAngleVector(double x)
{
return {cos(x), sin(x)};
}
//All of these would be 100% valid:
pair p = std::make_from_tuple<pair<double, double>, tuple<double,double>>(GetAngleVector(2.7));
array<double, 2> arr = std::make_from_tuple<std::array<double, 2>, tuple<double,double>>(GetAngleVector(2.7));
struct Point2D {
double x,y;
};
Point2D point = std::make_from_tuple<Point2D, tuple<double,double>>(GetAngleVector(2.7))
pair p = [2,"Hello world"];
array<double, 3> arr = [3.7, 9.2, 8.1];
pair p = {2, "Hello world"};
array<double, 3> arr = {3.7, 9.2, 8.1};
[double, double] GetAngleVector(double x)
{
return [cos(x), sin(x)];
}
tuple t = GetAngleVector(2.7);
pair p = GetAngleVector(2.7);
array<double, 2> arr = GetAngleVector(2.7);
template<class T> T GetAngleVector(double x)
{
return T{cos(x), sin(x)};
}
tuple t = GetAngleVector<tuple<double, double>>(2.7);
pair p = GetAngleVector<pair<double, double>>(2.7);
array<double, 2> arr = GetAngleVector<array<double, 2>>(2.7);
auto [x,y] = [3,4.7];
auto [x,y] = {3,4.7};//Currently causes error
Structured binding is an extremely useful concept because it allows the efficent and intuitive unpacking of data-structures into their individual values, and it works on a wide range of data structures. Not only does it work on tuples and pairs, but it also works on structs defined by the programmer without any additional effort on their part. I propose making this language feature more complete by allowing structured binding to take place in reverse:
tuple a = {1, 3.7}; //Creates a tuple of type tuple<int, double> with values 1 and 3.7
tuple b = [1, 3.7]; //Ditto
Where is this useful?The above example isn't particularly useful, HOWEVER here's a more real usage case:
[double, double] GetAngleVector(double x)
{
return [cos(x), sin(x)];
}
//All of these would be 100% valid:
tuple t = GetAngleVector(2.7);
pair p = GetAngleVector(2.7);
array<double, 2> arr = GetAngleVector(2.7);
auto [x,y] = GetAngleVector(2.7);
struct Point2D {
double x,y;
};
Point2D point = GetAngleVector(2.7);
Here, the function returns a structured-binding that will automatically map to whatever it's assigned to without issue.
struct Point2DR {
double y,x;
};
The following will be valid
as well?
Point2DR point = GetAngleVector(2.7);
And
Point2DR point =
Point2D{0.5,
1.2};
I don't want these implicit constructions.
Something similar could be achieved in standard C++17, although it's unseemly and difficult to read:
tuple<double, double> GetAngleVector(double x)
{
return {cos(x), sin(x)};
}
//All of these would be 100% valid:
pair p = std::make_from_tuple<pair<double, double>, tuple<double,double>>(GetAngleVector(2.7));
array<double, 2> arr = std::make_from_tuple<std::array<double, 2>, tuple<double,double>>(GetAngleVector(2.7));
struct Point2D {
double x,y;
};
Point2D point = std::make_from_tuple<Point2D, tuple<double,double>>(GetAngleVector(2.7))
tuple<double, double> GetAngleVector(double x)
{
return {cos(x), sin(x)};
}
pair
p = product_type::make_from<pair>(GetAngleVector(2.7));
array<double, 2> arr = product_type::make_from<array_tc>
(GetAngleVector(2.7));
struct Point2D {
double x,y;
};
Point2D point = product_type::
make_from<Point2D>(GetAngleVector(2.7))
I'll repeat my comment on this thread, in case you want to say what do you think of P0341?.
Maybe you will be interested in
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0341r0.html
In particular the pack types and the named pack types are very similar
to what you are proposing in your paper ;-)