Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, the code is standard conforming. The <code>+</code> triggers a conversion to a plain old function pointer for the lambda.</p> <p>What happens is this:</p> <p>The compiler sees the first lambda (<code>[]{}</code>) and generates a closure object according to §5.1.2. As the lambda is a <strong>non-capturing</strong> lambda, the following applies:</p> <blockquote> <h3>5.1.2 Lambda expressions [expr.prim.lambda]</h3> <p><sup>6</sup> The closure type for a <em>lambda-expression</em> with no <em>lambda-capture</em> 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>This is important as the unary operator <code>+</code> has a set of built-in overloads, specifically this one:</p> <blockquote> <h3>13.6 Built-in operators [over.built]</h3> <p><sup>8</sup> For every type <code>T</code> there exist candidate operator functions of the form</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;<code>T* operator+(T*);</code></p> </blockquote> <p>And with this, it's quite clear what happens: When operator <code>+</code> is applied to the closure object, the set of overloaded built-in candidates contains a conversion-to-any-pointer and the closure type contains exactly one candidate: The conversion to the function pointer of the lambda.</p> <p>The type of <code>test</code> in <code>auto test = +[]{};</code> is therefore deduced to <code>void(*)()</code>. Now the second line is easy: For the second lambda/closure object, an assignment to the function pointer triggers the same conversion as in the first line. Even though the second lambda has a different closure type, the resulting function pointer is, of course, compatible and can be assigned.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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