[PATCH] Changing lambda function pointer conversion operator to constexpr

Andy Gibbs andyg1001 at hotmail.co.uk
Tue Jun 3 10:14:12 PDT 2014


Hi,

Attached is a patch which I would like to put forward to relax the constexpr 
restriction on the lambda function pointer conversion operator.

Currently, it is not possible to cast a non-capturing lambda to its function 
pointer form at compile time.  This means the following code will not work:

struct Table {
  // ...
  int (*fn)();
  // ...
};

constexpr Table table[] = {
  // ...
  { .fn = []{ return 123; } },
  // ...
};

I have tried to find a clear rationale as to why this should not work, but 
without real success.  The closest I have come to is that lambda expressions 
*themselves* may not be constexpr.  However, this doesn't mean that the 
conversion operator itself should be so restricted, IMHO, and certainly the 
use-case above is clearer using lambdas than the alternative of static 
global functions.

The attached patch therefore lifts this restriction so that the conversion 
operator itself is constexpr (a one-line change in SemaLambda.cpp but also 
requiring LambaExpr be supported in TemporaryExprEvaluator in 
ExprConstant.cpp).

Note that this patch *does not* permit calling a lambda expression through 
its function pointer in a constexpr evaluation context -- this restriction 
remains as before.

I think this is a reasonable and valid change.  Please could someone look at 
this for me and let me know if it is worth committing and if the approach is 
right!

Cheers
Andy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: lambda.diff
Type: application/octet-stream
Size: 10086 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140603/1f6445c1/attachment.obj>


More information about the cfe-commits mailing list