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

Attachment: lambda.diff
Description: Binary data

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to