auto [] = whatever;
--
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-proposal...@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/dae03b23-a771-4844-90dc-d3de39c03d47%40isocpp.org.
Basically, just allow us to do this:
auto [] = whatever;
If you make that legal for all types, then what you effectively have is an unnamed variable. Structured binding/decomposition declarations already require that ability as a side effect of their primary work. We would just be using that side effect directly.
Can you provide an example on how you would use the unnamed variable?
And presto: we allow people to have unnamed variables.
--
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/e0d4144c-421e-d89f-10ce-155e16ee5db2%40wanadoo.fr.
On Thursday, January 12, 2017 at 12:51:30 AM UTC-5, TONGARI J wrote:2017-01-12 9:34 GMT+08:00 Nicol Bolas <jmck...@gmail.com>:Basically, just allow us to do this:
auto [] = whatever;
If you make that legal for all types, then what you effectively have is an unnamed variable. Structured binding/decomposition declarations already require that ability as a side effect of their primary work. We would just be using that side effect directly.I'm not against the idea but it's not a general solution for unnamed variable, e.g. "whatever" may not be moveable/copyable.
... so?
We're talking post-C++17. Which means post-guaranteed elision.
On Wed, Jan 11, 2017 at 7:34 PM, Nicol Bolas <jmck...@gmail.com> wrote:
> auto [] = whatever;
I see 2 problems this suggestion.
1. It has to be a full statement because it is a definition,
so it cannot appear in an expression to extend the lifetime
of the result of a sub-expression, which is an often wanted
feature.
2. If in the future we allow an explicit type T to appear in
place of the `auto` in structured binding, `T [] = expr`
may produce meaningless effects, banning them
will make your seemly natural suggestion unnatural.
On Thursday, January 12, 2017 at 2:14:34 AM UTC-5, Zhihao Yuan wrote:On Wed, Jan 11, 2017 at 7:34 PM, Nicol Bolas <jmck...@gmail.com> wrote:
> auto [] = whatever;
I see 2 problems this suggestion.
1. It has to be a full statement because it is a definition,
so it cannot appear in an expression to extend the lifetime
of the result of a sub-expression, which is an often wanted
feature.
This idea is not intending to solve those cases. I brought this up because structured binding has 99% of the infrastructure needed for a commonly-asked-for feature: declaring variables with no accessible/visible names.
The case of extending the lifetime of some sub-expression is a different problem requiring a different solution.
What "meaningless effects" would `T [] = expr` produce?2. If in the future we allow an explicit type T to appear in
place of the `auto` in structured binding, `T [] = expr`
may produce meaningless effects, banning them
will make your seemly natural suggestion unnatural.
On Jan 12, 2017 9:19 AM, "Nicol Bolas" <jmck...@gmail.com> wrote:On Thursday, January 12, 2017 at 2:14:34 AM UTC-5, Zhihao Yuan wrote:On Wed, Jan 11, 2017 at 7:34 PM, Nicol Bolas <jmck...@gmail.com> wrote:
> auto [] = whatever;
I see 2 problems this suggestion.
1. It has to be a full statement because it is a definition,
so it cannot appear in an expression to extend the lifetime
of the result of a sub-expression, which is an often wanted
feature.
This idea is not intending to solve those cases. I brought this up because structured binding has 99% of the infrastructure needed for a commonly-asked-for feature: declaring variables with no accessible/visible names.
The case of extending the lifetime of some sub-expression is a different problem requiring a different solution.But any solution which can extend the lifetime of a sub-expression(to match the lifetime of a named variable) can smoothly extendthe lifetime of an expression as initializer, making your solutionredundant.
What "meaningless effects" would `T [] = expr` produce?2. If in the future we allow an explicit type T to appear in
place of the `auto` in structured binding, `T [] = expr`
may produce meaningless effects, banning them
will make your seemly natural suggestion unnatural.bool [] = expr; where expr returns a RAII object convertibleto bool, then the RAII effort loses in vain.
The principle advantage of what I'm talking about is that we almost have it already. The wording and so forth for the most complex part has been worked out, and there is viable syntax which already exists. Therefore, the syntactic and standardization burden is minimal.
By contrast, what you're talking about is a completely new construct: arbitrary expression lifetime extension. If that new construct were something that was on the cusp of standardization, I probably wouldn't have made this suggestion. But I don't recall any proposals about generalized expression lifetime extension. I don't even recall any threads on it here.
[...]
Right, but the results of that would be no different from you doing `bool b = expr;`. So there's no "meaningless effect"; you just did the wrong thing.
Remember: the goal is not to extend the lifetime of `expr`; the goal is to have an unnamed variable.
On Jan 12, 2017 12:29 PM, "Nicol Bolas" <jmck...@gmail.com> wrote:
The principle advantage of what I'm talking about is that we almost have it already. The wording and so forth for the most complex part has been worked out, and there is viable syntax which already exists. Therefore, the syntactic and standardization burden is minimal.
By contrast, what you're talking about is a completely new construct: arbitrary expression lifetime extension. If that new construct were something that was on the cusp of standardization, I probably wouldn't have made this suggestion. But I don't recall any proposals about generalized expression lifetime extension. I don't even recall any threads on it here.
[...]
Right, but the results of that would be no different from you doing `bool b = expr;`. So there's no "meaningless effect"; you just did the wrong thing.The principle advantage losses when wediscovered meaningless combinations,meaningless combinations implyartificial orthogonality, hence highdegree coupling.
And in principle there is a high degreecoupling: unnamed variable will becoupled with structured binding, can onlybe used at where structured bindingmay be used, and has to be extended toeverywhere structured binding beingextended.
Remember: the goal is not to extend the lifetime of `expr`; the goal is to have an unnamed variable.C++ already has true unnamed objectthat is temporary object,
and differencebetweenf();auto [] = f();is merely whether the lifetime of thereturn value of f() is long enough toreach the end of the scope. So why notcall it out?__extend_me f();T a = g(__extend_me f());Very straightforward, right? Attackingthe core of the problem.
for(auto [] : ints(5))
//Do this 5 times.
On Thursday, January 12, 2017 at 1:56:41 PM UTC-5, Zhihao Yuan wrote:
Right, but the results of that would be no different from you doing `bool b = expr;`. So there's no "meaningless effect"; you just did the wrong thing.The principle advantage losses when wediscovered meaningless combinations,meaningless combinations implyartificial orthogonality, hence highdegree coupling.
... I'm not sure I understand what you're saying. Because it sounds like you're saying that the existence of `bool b = expr` is a problem with the language. [...]
Because the only difference between `bool [] = expr` and `bool b = expr` is that you gave it a name. So if one of these is wrong, then the other must be wrong for the same reasons. Accordingly, if one of these is fine, then the other must also be fine.
You cannot be fine with one while claiming that the other is wrong.
for(auto [] : ints(5))
//Do this 5 times.
My idea does.
I also don't know how `__extend_me` would impact globals, [...]
Can `__extend_me` work with `constexpr` things? Because decomposition declarations can (or if not now, they certainly will once that gets fixed).
Can `__extend_me` temporaries have attributes?
Do you see now why using structured binding here works so well?
Therefore, using `__extend_me` in various cases can require copy/move constructors.
On Jan 12, 2017 1:57 PM, "Nicol Bolas" <jmck...@gmail.com> wrote:On Thursday, January 12, 2017 at 1:56:41 PM UTC-5, Zhihao Yuan wrote:
Right, but the results of that would be no different from you doing `bool b = expr;`. So there's no "meaningless effect"; you just did the wrong thing.The principle advantage losses when wediscovered meaningless combinations,meaningless combinations implyartificial orthogonality, hence highdegree coupling.
... I'm not sure I understand what you're saying. Because it sounds like you're saying that the existence of `bool b = expr` is a problem with the language. [...]
Because the only difference between `bool [] = expr` and `bool b = expr` is that you gave it a name. So if one of these is wrong, then the other must be wrong for the same reasons. Accordingly, if one of these is fine, then the other must also be fine.
You cannot be fine with one while claiming that the other is wrong.bool b = expr is right and bool [] = expris wrong, because the goal of the formeris to transfer value,
the side effect isa non-goal, while the goal of the later isRAII, but it lost RAII, so it's wrong.
Can `__extend_me` work with `constexpr` things? Because decomposition declarations can (or if not now, they certainly will once that gets fixed).constexpr auto [] = an excellent exampleof meaningless combinations becauseobjects with nontrival destructor doesnot work with constexpr.
On Thu, Jan 12, 2017 at 1:56 PM, Nicol Bolas <jmck...@gmail.com> wrote:
> Do you see now why using structured binding here works so well? It already
> has answered all of the questions that need to be answered.
...
So what is `auto [] = f();`'s semantics? There is no `a` and
`b`, no name is referring to the return value of f(), so why
the return value cannot be immediately destroyed? Why
the whole thing is different from `f();`?
On Thu, Jan 12, 2017 at 5:31 PM, Nicol Bolas <jmck...@gmail.com> wrote:
>
> You're concerned about someone making a mistake that is highly unlikely to
> actually happen. I don't see why we should be concerned that someone could
> type the wrong thing, so long as:
>
> 1: They can type the right thing.
>
> 2: Typing the right thing requires less effort than the wrong thing.
>
> 3: It will be immediately obvious upon reading the code when they've typed
> the wrong thing.
>
Yes, real person is not going to write that, but it is still true that
your solution opens a whole can of meaningless thingy.
`bool b = true;` is meaningful, but `bool [] = true;` is meaningless
and `bool [] = f();` is meaningless -- there is no `bool [] = ...;` that
is meaningful.
> I've proposed a mechanism that solves the problem it is intended to solve,
> that is simple to use, that is easily understood, that follows existing
> constructs in the language, and that requires minimal effort to standardize
> (in terms of wording).
>
And I showed that:
1. Coupling your solution to structured binding may be risky
2. Designing based on existing specification may be missing the
point of the existing facility.
auto [first, [], third] = expr;
auto [ [], [inner_first, inner_second], third ] = expr;
It would also work out naturally if we expand decompositions into multiple levels:
auto [ [], [inner_first, inner_second], third ] = expr;
So the syntax is not really as much of a hack as I thought.
But you can't use _ to mean an anonymous variable. _ is already a
valid name and used by some for various purposes.
Maybe some combination of keywords can be used for anonymous variables. "auto auto = foo()" sorta makes sense... an automatic type followed by an automatic name. Or maybe "auto default = foo()".
auto[ auto [], b, auto [c, d]] = foo();
Maybe we are talking about different things, I was refering to the suggested use of nested empty brackets to skip binding individual elements in a structured binding assignment so your raii example would be:auto [?] = get_raii();or if get_raii can return any number of things:auto [*] = get_raii();or maybe:auto [...] = get_raii();But now that you put it forth it does seem logical to allow your case too if get_raii returns a single value thus separating the ideas of using ? as a unnamed variable from the structured binding concept, the ? becomes just a reusable anonymous name, which is something that awkward and often unsafe macros have been used for so far.At this point and given the fact that the element counts in structured binding must match my interpretation of the [] suggestion as a degenerate case of nested structured binding does not work, and it seems very hard to explain this difference:auto [] = get_raii(); // Fails as get_raii does not return 0 values.auto [ [] ] = get_raii() // Succeeds as the inner empty brace goes by some other rule?
Going back to your original proposal it seems that you want to use a rule of implicitly throwing away excessive RHS elements, in this case the one and only.
But according to Matthew Woehlke a few posts up the number of elements on the LHS and RHS of a structured binding assignment must match.
So it seems that if this rule was changed your proposal would be a natural consequence, but with the rule as stated I don't find in particularly logical,