Topic: Overloading `using` for temporary scopes


Author: Matthew Woehlke <mwoehlke.floss@gmail.com>
Date: Mon, 28 Jan 2019 11:20:39 -0500
Raw View
I often wish C++ had the equivalent of Python's `with`:

  with (auto x = expr())
  {
    // use 'x' here
  }
  // 'x' is out of scope

With C++17 I can more or less achieve this:

  if (auto x = expr(); true)
  {
    // use 'x' here
  }
  // 'x' is out of scope

....and even:

  #define with(...) if (__VA_AGS__; true)

....but I still have to declare and name the variable, even if I don't
actually care (e.g. lock guards). While we will hopefully, eventually
solve the 'anonymous variable' problem, I wonder if there would be any
perceived value in having a dedicated syntax for this? I'm thinking either:

  with (auto x = expr())
  with (expr())

....where in the latter case, the result of `expr()` is "live" until the
end of the `with` scope, e.g. like `if (auto&& ? = expr(); true)`. Or even:

  using (auto x = expr())
  using (expr())

The benefit of the latter, of course, is that it does not need a new
keyword. The benefit of *either* is that, as a language feature, it
could be made to work with either an expression or a declaration, which,
AFAICT, is not possible with any macro-based implementation.

Does anyone think this would be useful? Or is it not worthwhile given
the work-arounds that exist?

--
Matthew

--
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-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/41a2bed6-6738-cd11-0037-9a471f8a75d5%40gmail.com.

.


Author: David Brown <david@westcontrol.com>
Date: Tue, 29 Jan 2019 15:26:29 +0100
Raw View
On 28/01/2019 17:20, Matthew Woehlke wrote:
> I often wish C++ had the equivalent of Python's `with`:
>
>   with (auto x = expr())
>   {
>     // use 'x' here
>   }
>   // 'x' is out of scope
>
> With C++17 I can more or less achieve this:
>
>   if (auto x = expr(); true)
>   {
>     // use 'x' here
>   }
>   // 'x' is out of scope
>
> ...and even:
>
>   #define with(...) if (__VA_AGS__; true)
>
> ...but I still have to declare and name the variable, even if I don't
> actually care (e.g. lock guards). While we will hopefully, eventually
> solve the 'anonymous variable' problem, I wonder if there would be any
> perceived value in having a dedicated syntax for this? I'm thinking either:
>
>   with (auto x = expr())
>   with (expr())
>
> ...where in the latter case, the result of `expr()` is "live" until the
> end of the `with` scope, e.g. like `if (auto&& ? = expr(); true)`. Or even:
>
>   using (auto x = expr())
>   using (expr())
>
> The benefit of the latter, of course, is that it does not need a new
> keyword. The benefit of *either* is that, as a language feature, it
> could be made to work with either an expression or a declaration, which,
> AFAICT, is not possible with any macro-based implementation.
>
> Does anyone think this would be useful? Or is it not worthwhile given
> the work-arounds that exist?
>

Basically, what you want is to be able to give "expr()" a name so that
it stays in scope, but you don't want to give it a name because it isn't
something you are going to use.  I don't know of any neat way to do this.

#define anon anon##__counter__

{
 auto anon = expr();
 auto anon = expr2();
 T1 anon;

 // These are all still in scope
}


--
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-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/q2pnql%241mvo%241%40blaine.gmane.org.

.


Author: Matthew Woehlke <mwoehlke.floss@gmail.com>
Date: Tue, 29 Jan 2019 11:42:15 -0500
Raw View
On 29/01/2019 09.26, David Brown wrote:
> On 28/01/2019 17:20, Matthew Woehlke wrote:
>> I often wish C++ had the equivalent of Python's `with` [...] I
>> wonder if there would be any perceived value in having a dedicated
>> syntax for this? I'm thinking [e.g.]:
>>
>>   with (auto x = expr())
>>   with (expr())
>
> Basically, what you want is to be able to give "expr()" a name so that
> it stays in scope, but you don't want to give it a name because it isn't
> something you are going to use.

Yes. But *sometimes*. Eventually I think we will get a way to declare an
anonymous variable; too many people have wanted it for too long. The
reason my proposal might be interesting is because it is one syntax that
allows *either* naming the temporary or not naming it.

--
Matthew

--
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-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/de706d53-f3c1-1810-ff61-cc6cc4f40270%40gmail.com.

.