Leaking the local class outside function

55 Views Asked by At

I have a function named return_local where it returns an instance of a local class enclosed with a lambda.

auto return_local() {
  return [] {
    static int sample { 5 };
    struct Point {
      int x, y;
      int show_sample() { return sample; }
    };
    return Point {};
  }();
}
using Point = decltype(return_local());

Now, I can use Point as alias with characteristics of local classes.

Point p {.x = 4, .y = 1};
p.show_sample(); // returns 5

Is this code normal? Are there any benefits of using this "leaked" class?

0

There are 0 best solutions below