Topic: lambdas N2413, named?


Author: skaller <skaller@users.sourceforge.net>
Date: Tue, 16 Oct 2007 13:26:48 CST
Raw View
Perhaps I missed something in N2413, but why are only anonymous lambdas
supported?

This seems to be a special case. The proposal effectively allows
nested functions, so why not allow them to be named?

More generally ..  Why not simply allow nested lexically scoped
functions?


--
John Skaller <skaller at users dot sf dot net>
Try Felix, the successor to C++ http://felix.sf.net

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: gpderetta <gpderetta@gmail.com>
Date: Wed, 17 Oct 2007 11:05:00 CST
Raw View
On Oct 16, 9:26 pm, skaller <skal...@users.sourceforge.net> wrote:
> Perhaps I missed something in N2413, but why are only anonymous lambdas
> supported?
>
> This seems to be a special case. The proposal effectively allows
> nested functions, so why not allow them to be named?
>
> More generally ..  Why not simply allow nested lexically scoped
> functions?
>

What about:

  void foo() {
      auto named = <>(a, b) { a + b };
 }

Doesn't auto do what you are asking for?

--

Giovanni P. Deretta

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: int19h@gmail.com
Date: Wed, 17 Oct 2007 11:39:04 CST
Raw View
On 16 Oct, 23:26, skaller <skal...@users.sourceforge.net> wrote:
> Perhaps I missed something in N2413, but why are only anonymous lambdas
> supported?

Sure you can have named lambdas:

void foo(int x) {
  auto bar = <&>(int y) (x + y);
  ...
}

> More generally ..  Why not simply allow nested lexically scoped
> functions?

Because, for consistency, unary operator& should also work for them
and give result of plain function pointer type (i.e. just a single
code pointer). Which, while doable (gcc does it), requires very ugly
hacks with associated perfomance penalties on a lot of platforms.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Yechezkel Mett <ymett.on.usenet@gmail.com>
Date: Wed, 17 Oct 2007 11:39:55 CST
Raw View
On Oct 16, 9:26 pm, skaller <skal...@users.sourceforge.net> wrote:
> Perhaps I missed something in N2413, but why are only anonymous lambdas
> supported?
>
> This seems to be a special case. The proposal effectively allows
> nested functions, so why not allow them to be named?

You can assign a lambda to a named variable (with type deduction):

auto square = <>(int i)->int { return i * i; };

> More generally ..  Why not simply allow nested lexically scoped
> functions?

To give a nested function access to the surrounding stack frame
requires either a special kind of function pointer or run-time
generation of a thunk. I think some platforms may have difficulty with
run-time code generation, and a new kind of function pointer means a
new type, incompatible with current function pointers -- which is
effectively what is proposed, with the addition of allowing inline,
anonymous definitions, which is what many people wanted from lambdas
for use with algorithms.

Also it was considered dangerous to allow uneeded by-reference access
to the stack variables which will go out of scope, so the proposal
only gives access to variables if explicitly requested. This would not
be possible with plain nested functions.

Yechezkel Mett

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: gpderetta <gpderetta@gmail.com>
Date: Wed, 17 Oct 2007 15:24:30 CST
Raw View
On Oct 16, 9:26 pm, skaller <skal...@users.sourceforge.net> wrote:
> Perhaps I missed something in N2413, but why are only anonymous lambdas
> supported?
>
> This seems to be a special case. The proposal effectively allows
> nested functions, so why not allow them to be named?
>
> More generally ..  Why not simply allow nested lexically scoped
> functions?
>

Sure they can be named:

  void foo() {
     auto named_lambda = <>(a, b){  a + b }
  }

Doesn't that do exactly what you want?

gpd


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]