Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes I believe the first example is safe, regardless of the life-time of all the temporaries created during the evaluation of the full-expression that involves the capture-less lambda-expression.</p> <p>Per the working draft (n3485) 5.1.2 [expr.prim.lambda] p6 </p> <blockquote> <p>The closure type for a lambda-expression with no lambda-capture 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.</p> </blockquote> <p>The above paragraph says nothing about the pointer-to-function's validity expiring after evaluation of the lambda-expression.</p> <p>For e.g., I would expect the following to work:</p> <pre><code>auto L = []() { return [](int x, int y) { return x + y; }; }; int foo( int (*sum)(int, int) ) { return sum(3, 4); } int main() { foo( L() ); } </code></pre> <p>While implementation details of clang are certainly not the final word on C++ (the standard is), if it makes you feel any better, the way this is implemented in clang is that when the lambda expression is parsed and semantically analyzed a closure-type for the lambda expression is invented, and a static function is added to the class with semantics similar to the function call operator of the lambda. So even though the life-time of the lambda object returned by 'L()' is over within the body of 'foo', the conversion to pointer-to-function returns the address of a static function that is still valid.</p> <p>Consider the somewhat analagous case:</p> <pre><code>struct B { static int f(int, int) { return 0; } typedef int (*fp_t)(int, int); operator fp_t() const { return &amp;f; } }; int main() { int (*fp)(int, int) = B{}; fp(3, 4); // You would expect this to be ok. } </code></pre> <p>I am certainly not a core-c++ expert, but FWIW, this is my interpretation of the letter of the standard, and I feel it is defendable.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload