c***@gmail.com
2018-10-22 19:27:10 UTC
Hi,
I have an API that requires passing an object which implements multiple
callbacks.
For instance:
template<typename Callbacks> void myAPI(Callbacks &&c)
{
c.foo();
c.bar();
}
When I use it, I end up doing something like this:
void useMyAPI() {
int i;
class MyCallbacks {
MyCallbacks(int &i): i(i) {}
int &i;
void foo() { ++ i; }
void bar() { --i; }
};
myAPI(MyCallbacks{i});
}
This can be cumbersome if I want to capture more local variables.
So, to solve this problem, I'd like to allow captures in local class
definitions.
To make this work, I would:
- make the classes unnamed
- force the class to be used immediately to declare a variable or
generate a temporary
For instance, something like this:
void useMyAPI() {
int i = 0;
myAPI(class [&] {
void foo() { ++ i; }
void bar() { --i; }
}());
}
This would also be useful to generate a class implementing a specific
interface using inheritance.
Incidentally, lambdas could then be defined in terms of a capture-class
with an operator().
A useful addition would be to allow putting destructors in them and, in
that case, allow an implicit, unnamed variable declaration, for easy
RAII, for instance like this:
int *p = (int *)malloc(10);
class [p] {
~() { free(p); }
};
Anyway, so my questions are:
- Has something like this already been proposed? It's not easy to search
for so I didn't find anything in 5 minutes on google
- If not, does it sound interesting? If so I could work on a paper
Thanks!
I have an API that requires passing an object which implements multiple
callbacks.
For instance:
template<typename Callbacks> void myAPI(Callbacks &&c)
{
c.foo();
c.bar();
}
When I use it, I end up doing something like this:
void useMyAPI() {
int i;
class MyCallbacks {
MyCallbacks(int &i): i(i) {}
int &i;
void foo() { ++ i; }
void bar() { --i; }
};
myAPI(MyCallbacks{i});
}
This can be cumbersome if I want to capture more local variables.
So, to solve this problem, I'd like to allow captures in local class
definitions.
To make this work, I would:
- make the classes unnamed
- force the class to be used immediately to declare a variable or
generate a temporary
For instance, something like this:
void useMyAPI() {
int i = 0;
myAPI(class [&] {
void foo() { ++ i; }
void bar() { --i; }
}());
}
This would also be useful to generate a class implementing a specific
interface using inheritance.
Incidentally, lambdas could then be defined in terms of a capture-class
with an operator().
A useful addition would be to allow putting destructors in them and, in
that case, allow an implicit, unnamed variable declaration, for easy
RAII, for instance like this:
int *p = (int *)malloc(10);
class [p] {
~() { free(p); }
};
Anyway, so my questions are:
- Has something like this already been proposed? It's not easy to search
for so I didn't find anything in 5 minutes on google
- If not, does it sound interesting? If so I could work on a paper
Thanks!
--
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/bf3a4a59-cc3f-4272-a239-80d7e28c4bf0%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/bf3a4a59-cc3f-4272-a239-80d7e28c4bf0%40isocpp.org.