2

我有一个名为的函数return_local,它返回一个包含 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());

现在,我可以使用Point具有本地类特征的别名。

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

这段代码正常吗?使用这个“泄露”的课程有什么好处吗?

4

0 回答 0