Francisco Lopes
2016-07-10 17:37:00 UTC
I'm moving the "deferred destruction"
<https://groups.google.com/a/isocpp.org/d/msg/std-proposals/8lsCinlSO1w/S6zsTqrYCQAJ>topic
here to make things clearer.
The current proposal idea I'm moving forward with allowing (semi) anonymous
variables.
The idea is to adopt _ as a special variable name that supports overriding,
but in a backwards
compatible way:
- _ should be allowed as a variable name.
- _ should be allowed in redeclarations, overriding the previous
declaration in the same scope.
This special variable would, for example, improve usage of:
- any kind of guards, like std::lock_guard for example
- multiple return (P0217R2)
- scope_exit (P0052R2)
Here's some example code showing these features:
#include <mutex>
#include <tuple>
#include <scope>
#include <thread>
#include <memory>
#include <iostream>
using namespace std;
mutex m1, m2;
tuple<int, int> divide(int dividend, int divisor) {
return make_tuple(dividend / divisor, dividend % divisor);
}
int main() {
auto _ = make_scope_exit([]{ cout << " bye"; }); // no need to care
about scope_exit naming
auto _ = make_scope_exit([]{ cout << "bye"; }); // _ is the only
variable name that supports being overriding
lock_guard<mutex> _(m1), _(m2); // it always refers to
the last declaration in the current scope
auto [_, remainder] = divide(14, 3); // works on multiple
return
}
Thanks Tony V E for the input in the previous thread
<https://groups.google.com/a/isocpp.org/d/msg/std-proposals/8lsCinlSO1w/JWkYRnndCQAJ>
.
<https://groups.google.com/a/isocpp.org/d/msg/std-proposals/8lsCinlSO1w/S6zsTqrYCQAJ>topic
here to make things clearer.
The current proposal idea I'm moving forward with allowing (semi) anonymous
variables.
The idea is to adopt _ as a special variable name that supports overriding,
but in a backwards
compatible way:
- _ should be allowed as a variable name.
- _ should be allowed in redeclarations, overriding the previous
declaration in the same scope.
This special variable would, for example, improve usage of:
- any kind of guards, like std::lock_guard for example
- multiple return (P0217R2)
- scope_exit (P0052R2)
Here's some example code showing these features:
#include <mutex>
#include <tuple>
#include <scope>
#include <thread>
#include <memory>
#include <iostream>
using namespace std;
mutex m1, m2;
tuple<int, int> divide(int dividend, int divisor) {
return make_tuple(dividend / divisor, dividend % divisor);
}
int main() {
auto _ = make_scope_exit([]{ cout << " bye"; }); // no need to care
about scope_exit naming
auto _ = make_scope_exit([]{ cout << "bye"; }); // _ is the only
variable name that supports being overriding
lock_guard<mutex> _(m1), _(m2); // it always refers to
the last declaration in the current scope
auto [_, remainder] = divide(14, 3); // works on multiple
return
}
Thanks Tony V E for the input in the previous thread
<https://groups.google.com/a/isocpp.org/d/msg/std-proposals/8lsCinlSO1w/JWkYRnndCQAJ>
.
--
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+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/505fe073-a45b-4974-b1cb-df03ea39a5c6%40isocpp.org.
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+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/505fe073-a45b-4974-b1cb-df03ea39a5c6%40isocpp.org.