Desmond Gold
Desmond Gold

Reputation: 2065

Leaking the local class outside function

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?

Upvotes: 2

Views: 54

Answers (0)

Related Questions