2021年1月2日星期六

Return type of closure in C++

I am trying to learn closures in C++. This example works:

auto add_n(int n) {      return [=](int x) -> int {          return x + n;      };  };  auto add_3 = add_n(3);  add_3(5); //8  

What type is add_n expecting out? I would expect to be able to use a function pointer that takes in an int and returns an int as the return type?

typedef int (*fptr)(int);  fptr add_n(int n) {      return [=](int x) -> int {          return x + n;      };   };  

Additionally, would anything else need to be done if I am working with templates instead? Thank you!

https://stackoverflow.com/questions/65545712/return-type-of-closure-in-c January 03, 2021 at 09:03AM

没有评论:

发表评论