> > Structured Bindings would be acceptable if, like scripting languages, we
> > didn't have anything else to work with.
> >
> > But we do: We can return a struct.
>
> Structured bindings work with structs as well. If you think structured
> bindings mean
> you must return a tuple, you're mistaken.
Yes, I know that. But if I return a struct with proper names, I don't need SB
to handle it. Indeed, it would be counter-productive to have to find names for
SB if can access the names the implementor chose with C-space in my IDE from
the auto variable that stores the return type.
> > I fear Structured Bindings will lead to an explosion of *really* bad API
> > that returns std::pair or std::tuple when it should return a small
> > struct with
>
> C++ programmers are not idiots.
C++ programmers are humans, though.
We all make mistakes. Even Alex Stepanov did. The art is to not repeat them.
On Wednesday 05 October 2016 13:40:20 Domen Vrankar wrote:
> So you're saying that returning a structure with error status and result or
> multiple results of which you only need one or two (reason being for eg.
> cheaper to return all at once - due to for e.g. web service call or complex
> processing - than request one by one on need to basis) you would still
> prefer dragging the entire structure around giving the feeling that your
> code has more dependencies than it actually does?
One of us is not underatanding SB correctly, or I don't understand you. SB
stores the whole struct, too, it just defines local aliases to the member of
the struct (or tuple, or pair, or ...).
& follows the decl-specifier-seq in the decomposition declaration, and is extended as usual (12.2 [class.temporary] otherwise.But SB works fine with this, because it allows the caller to write both this:
auto info = page.contains_info(ptr);
if (info.found == /*...*/) {
use(info.location, info.start_location);
}
and this:
auto [found, offset, start] info = page.contains_info(ptr);
if (found == /*...*/) {
use(offset, start);
}
On 5 October 2016 at 17:20, Greg Marr <greg...@gmail.com> wrote:
>> > Only if you say that you want references, using "auto &" or "auto &&".
>> > If
>> > you use "auto", you just get copies.
>>
>> You get a copy of the whole struct, not copies of individual bindings.
>
>
> That's not what's described in the paper. Has it changed since
That's what the wording of the facility does, and what the clang
implementation does.
> https://isocpp.org/files/papers/p0217r0.html ?
See http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0217r1.html
That paper incorporates feedback from the EWG session in Jacksonville as follows:
Hi Herb, Bjarne, Gabriel,
I heard about Structured Bindings for the first time at CppCon this year, and
I'd like to raise my concerns, esp. since half of the panel was crazy about
them.
I strongly believe that the premise that returning std::tuple from functions
to solve the multiple-return-types problem is good practice, is fundamentally
wrong.
It is not good in any sense of the word. It is horrible. Indeed, it is so
horrible that you felt inclined to add a new language feature just to make it
bearable.
The reaons it's horrible, is because it reverses the C++ principle that the
implementor of a library should have to do the work, not the user.
It reverses the principle by requiring the *caller* to choose the names of the
values returned, when it should be the implementor of the function who chooses
the names.
In conjunction with auto deduction, the caller choosing the names means that
the code becomes brittle in the face of changes to the return type, e.g. when
reordering fields to fill padding holes.
Structured Bindings would be acceptable if, like scripting languages, we
didn't have anything else to work with.
But we do: We can return a struct.
IMHO, the correct solution to the multiple return value problem is to return a
struct with properly named fields, not a pair and not a tuple.
I fear Structured Bindings will lead to an explosion of *really* bad API that
returns std::pair or std::tuple when it should return a small struct with
well-named data members instead, because a) the implementor couldn't be
bothered to pick good names for a return struct, and b) tuples can be defined
on the fly, in the function declaration, whereas structs can not.
I believe there's a proposal for allowing to define structs in function
declarations already, but I failed to find it.
On Wednesday, October 5, 2016 at 6:36:35 AM UTC-4, Marc Mutz wrote:
Hi Herb, Bjarne, Gabriel,
...
Structured binding is hardly the first C++ feature that makes struct field ordering important. Aggregate initialization did that long before. If you "reorder fields to fill padding holes" on an interface structure, then you've broken any code that used aggregate initialization on that type.
Good point. It has been like this since 1974 or so.
On Wednesday 05 October 2016 16:13:32 'Herb Sutter' via ISO C++ Standard -
Future Proposals wrote:
> > I heard about Structured Bindings for the first time at CppCon this year,
> > and I'd like to raise my concerns, esp. since half of the panel was
> > crazy about them.
>
> Thanks for your interest! Did you read the design paper? It answers many of
> these questions.
If with "design paper" you mean P0144R2, then, yes, I read it and, no, I'm
afraid it doesn't answer what SB is supposed to be good at except make
functions returning pairs and tuples bearable, syntax-wise, at the call site.
My main concern is that you shouldn't use these types in APIs in the first
place, and they are the current work around for the clumsiness of defining
structs to hold multiple return values.
> > The reaons it's horrible,
>
> Note that you're making a strong value judgment about something you heard
> about two weeks ago.
You misunderstand. The horrible thing is pair and tuple in APIs
typedef QPair<double, QColor> QGradientStop;QGradientStop s = ...
if(get<double>(s) < dist)
{
...
}
To summarise: I feel that Structured Bindings are too easy to use incorrectly,
because they put the burden of choosing a name for the fields of a return
value on the caller, instead of the implementor, of the function. I'd like to
see the pattern to return structs from functions strengthened, not the anti-
pattern of returning pairs and tuples.
It seems to me that the same will be true of tuples and structured bindings
once C++ gets the missing pieces: data member generation from template packs
(as part of reflection?), structs declared within the function declaration,
maybe Herb's multiple return values,...
std::tuple<int.a, int.b> f()
{
return {0, 1};
}
auto t = f();
assert(t.a == 0);
assert(t.b == 0);On Thursday 06 October 2016 00:07:03 Nicol Bolas wrote:
> On Wednesday, October 5, 2016 at 1:06:51 PM UTC-4, Marc Mutz wrote:
> > On Wednesday 05 October 2016 16:13:32 'Herb Sutter' via ISO C++ Standard
> >
And what do you do in this case:
struct GammaCorrectionStop {
uchar from;
uchar to;
};
?
> There's a lot of space between "not good" and "bad." And in many cases,
> pair/tuple use, while "not good", doesn't descend to the level of "bad".
>
> And that's good enough.
But is "good enough" good enough a reason to add it to the standard?
for(auto[x, y] : zip(vector1, vector2))
{
...
}
We have
bind, then came lambdas. Some bind expressions still look better than their
corresponding lambda equivalents, as do some Boost.Lambda expressions, btw,
but by and large, bind() and Boost.Lambda are dead with C++14 polymorphic
lambdas.
It seems to me that the same will be true of tuples and structured bindings
once C++ gets the missing pieces: data member generation from template packs
(as part of reflection?), structs declared within the function declaration,
maybe Herb's multiple return values,...
struct params
{
int x; int y; int z = 2;
};
void func(params p);
func({.x = ..., .y = ...});template<typename ...Ts>
struct {zip_range<Ts> &ranges...;} zip(Ts&& ...ts) {...}auto z_ranges = zip(vector1, vector2);
z_ranges.ranges[0]; //represents vector1auto z_ranges = zip(vector1, vector2);
get<0>(z_ranges); //Represents vector1On Thursday, October 6, 2016 at 5:40:20 PM UTC+8, Marc Mutz wrote:[...]It seems to me that the same will be true of tuples and structured bindings
once C++ gets the missing pieces: data member generation from template packs
(as part of reflection?), structs declared within the function declaration,
maybe Herb's multiple return values,...I have no idea how the multiple return values that Herb mentioned will differ from SB. A language that has built-in support for multiple return values
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANh8DEnqePPzAUgBGnST3aDEEyb05fwTvLtThU7z6AUuqbBKwQ%40mail.gmail.com.
My understanding is that from the OP's perspective, built-in tuple or some similar feature would address broader range of solutions than SB and involves less burden on the syntax.
On Thursday 06 October 2016 00:07:03 Nicol Bolas wrote:
But is "good enough" good enough a reason to add it to the standard? We have
bind, then came lambdas. Some bind expressions still look better than their
corresponding lambda equivalents, as do some Boost.Lambda expressions, btw,
but by and large, bind() and Boost.Lambda are dead with C++14 polymorphic
lambdas.