<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<title>Converting Lambdas to Function Pointers</title>
</head>
<body>
<h1>Converting Lambdas to Function Pointers</h1>

<p>
ISO/IEC JTC1 SC22 WG21 N3043 = 10-0033 - 2010-02-15
</p>

<p>
Lawrence Crowl, crowl@google.com, Lawrence@Crowl.org
<br>
Alisdair Meredith, public@alisdairm.net
</p>

<p>
This paper extracts a generally uncontroversial portion of
<a href="../../papers/2009/n2989.html">N2989</a> = 09-0179.
</p>


<h2>Introduction</h2>

<p>
The semantics of a lambda with an empty capture list and a function
are nearly uniform.
This uniformity was noted in U.K.&nbsp;national body comment 226.
Exposing this uniformity 
would allow programmers to use lambdas
and exploit existing function-based interfaces.
</p>
<blockquote>
<p>
<i>Problem:</i>
A lambda with an empty capture list has identical semantics
to a regular function type.
By requiring this mapping we get an efficient lambda type with a known API
that is also compatible with existing operating system and C library functions.
</p>
<p>
<i>Resolution:</i>
Add a new paragraph:
"A lambda expression with an empty capture set
shall be convertible to pointer to function type R(P),
where R is the return type and P is the parameter-type-list
of the lambda expression."
Additionally it might be good to
(a) allow conversion to function reference and
(b) allow extern "C" function pointer types.
</p>
</blockquote>

<p>
We adopt the suggested resolution.
</p>


<h2>Proposed Wording</h2>

<p>
The proposed wording shows changes from working draft
<a href="../../papers/2009/n3000.pdf">N3000</a>.
</p>


<h3><a name="expr.prim.lambda">5.1.2 Lambda expressions [expr.prim.lambda]</a></h3>

<p>
Edit paragraph 5 as follows.
The intent of this edit is
to obtain a closure-to-function-pointer conversion
with no <var>lambda-capture</var>.
</p>
<blockquote>
<p>
The closure type for a <var>lambda-expression</var>
has a public inline function call operator (13.5.4)
whose parameters and return type are described by the
<var>lambda-expression</var>'s <var>parameter-declaration-clause</var>
and <var>trailing-return-type</var> respectively.
<ins>A program taking the address of this function call operator
is ill-formed.</ins>
This function call operator
is declared <code>const</code> (9.3.1)
if and only if the <var>lambda-expression</var>'s
<var>parameter-declaration-clause</var>
is not followed by <code>mutable</code>.
It is not declared <code>volatile</code>.
Default arguments (8.3.6)
shall not be specified in the <var>parameter-declaration-clause</var>
of a <var>lambda-declarator</var>.
Any <var>exception-specification</var>
specified on a <var>lambda-expression</var>
applies to the corresponding function call operator.
Any <var>attribute-specifiers</var>
appearing immediately after the <var>lambda-expression</var>'s
<var>parameter-declaration-clause</var>
appertain to the type of the corresponding function call operator.
[<i>Note:</i>
Names referenced in the <var>lambda-declarator</var>
are looked up in the context in which
the <var>lambda-expression</var> appears.
&mdash;<i>end note</i>]
</p>
</blockquote>

<p>
Add a new paragraph after paragraph 5.
</p>

<blockquote>
<p><ins>
The closure type for a <var>lambda-expression</var>
with no <var>lambda-capture</var>
has a public non-virtual non-explicit const conversion function
to pointer to function having
the same parameter and return types
as the closure type's function call operator.
The value returned by this conversion function
shall be the address of a function that, when invoked,
has the same effect as invoking the closure type's function call operator.
</ins></p>
</blockquote>


</body>
</html>
