<div dir="ltr">Before I return to working on generic lambdas, I thought it might be useful to nail down DR 712 which reworded the definition of odr-use in terms of the potential results of an expression (in order to avoid odr-using a variable that represents a constant expression whenever we can get away with it).  This is relevant to lambdas, because it affects which variables get captured and which don't.<br>
<br><br><div><div><div class="gmail_extra">Let me review how I think clang currently handles this (pre-712 resolution  - please correct me where I err). While parsing and semanalyzing expressions, all variables that are usable in constant expressions (and so might not need to be odr-used) are stored in MaybeODRUseExprs (a set container) as DeclRefExprs (DRE).  Then, if an Lvalue-to-Rvalue conversion is performed on the DRE, it is removed from the set above (In UpdateMarkingForLValueToRValue).  Anything left in that set is marked as odr-used by ActOnFinishFullExpr.  <br>
<br><br></div><div class="gmail_extra">OK, with the above in mind, these are my initial thoughts on how to implement it so that not only do we handle those constant expressions that are lvalue-to-rvalue converted, but also those that are discarded, and also handle the conditional expressions and member-access expressions.<br>
<br></div><div class="gmail_extra">The relevant hooks/callbacks/functions within clang are:<br></div><div class="gmail_extra">  - UpdateMarkingForLValueToRValue<br></div><div class="gmail_extra">  - IgnoredValueConversions<br>
<br></div><div class="gmail_extra">A function i intend to write is GetPotentialResultsOfExpression: when passed an Expr* E, returns a vector of its potential results as DeclRefExpr's (and MemberExpr's??); this function should follow from  3.2 para 2.<br>
<br>Now in each hook, GetPotentialResults shall be called, and those expressions shall be removed from the MaybeODRUseExpr.<br><br></div><div class="gmail_extra">What are your thoughts on the above preliminary strategy?  I have this sense that Member expressions are going to require some more work, but we'll see...<br>
</div><div class="gmail_extra"><br></div><div class="gmail_extra">Also before I proceed any further, I was hoping to get some more clarity on 3.2 para 2 and para 3 - please see my queries below:<br><br><br><div class="gmail_extra">
Consider the following code:<br><br><br>struct S {<br>  const int mi;<br>  constexpr S(int i) : mi(i) { }<br>};<br><br></div><div class="gmail_extra">int f(const int& );<br>void g(int);<br>int main() {<br>  constexpr S r{5};<br>
  constexpr S s{r};<br>  [](char c) {  <br>      c = s.mi;  // #1 does this odr-use 's'<br></div><div class="gmail_extra">      g(s.mi);   //  #2 what about this?<br></div><div class="gmail_extra">      s.mi;        // #3 and this?<br>
</div><div class="gmail_extra">      c ? s.mi : r.mi; // #4 and this?<br></div><div class="gmail_extra">      g( c ? s.mi : r.mi ); // #5<br></div><div class="gmail_extra">  };<br>}<br></div><br></div><div class="gmail_extra">
My intial suspicion was that neither s nor r were odr-used in the lambda (because the compiler can figure out the value of 'mi' at its point of use, and use it or discard it) - but then based on Richard's analysis of the example below where he states that s.operator int() is an odr-use of 's', i am now unsure.<br>
<br>Here is my attempt at trying to figure this out (e refers to expression, ex as a potentially-evaluated expression, using the language of 3.2 para 2 and 3)<br><br></div><div class="gmail_extra">In #1, <br></div><div class="gmail_extra">
  the variable 's' appears as a potentially evaluated expression and satisfies the requirements for appearing in a constant expression, and it is an object, and it is an element of the set of potential results of an expression 'e' (s.mi) and the lvalue-to-rvalue conversion is applied to that expression so it is not odr-used?  But what if 'e' is deemed to be 's' in 's.mi' - no lvalue-to-rvalue conversion is applied so it should be captured?<br>
<br></div><div class="gmail_extra">OK, I'm going to give up on analyzing the rest, because I feel I'm missing something and am unable to think about this clearly.<br><br></div><div class="gmail_extra">Any help or guidance will be appreciated!<br>
<br> </div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 15, 2013 at 3:21 PM, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=""><div class="h5">On Tue, May 14, 2013 at 9:48 PM, Faisal Vali <span dir="ltr"><<a href="mailto:faisalv@gmail.com" target="_blank">faisalv@gmail.com</a>></span> wrote:<br>
</div></div><div class="gmail_quote"><div><div class="h5"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div dir="ltr"><br><div class="gmail_quote"><br><div>While we're on the topic, can i ask you to clarify a few capture and constexpr <br>questions below:<br></div></div></div></blockquote></div></div><div><div class="h5">
<div><br></div></div></div><div>OK, the relevant context is [basic.def.odr]p2 and p3.</div><div class="im">
<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div>struct S {</div>
<div>  constexpr S() {}</div>
<div>  constexpr operator int() const { return 0; }<br>
</div><div>};<br></div><div>void fooS(S s) { }<br></div><div><br>void fooInt(int i) { }<br><br></div><div><div>void f() {</div><div>  constexpr S s {};</div></div><div>  auto L = [] (int x) {</div>      (void) s;  // 1<br>

</div></div></div></div></blockquote><div><br></div></div><div>Here, the 's' expression is a discarded-value expression, and the 's' variable is in the set of potential results of the 's' expression, and 's' satisfies the constraints for appearing in a constant expression, so 's' is not odr-used, so is not captured. We should not require a capture-default here.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div></div><div>
      fooS(s);  // 2<br></div></div></div></div></blockquote><div><br></div><div>Here, 's' is implicitly passed to the implicit S::S(const S&) constructor. This is neither an lvalue-to-rvalue conversion nor a discarded-value expression, so 's' is odr-used and we have an error due to the missing capture-default.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div></div><div>      fooInt(s); // 3<br>
</div>
</div></div></div></blockquote><div><br></div><div>This is equivalent to 's.operator int()', which again odr-uses 's', so requires a capture-default.</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div></div><div><div>  };<br></div><div>}<br><br></div><div>Should the above be ok in spite of L not having a default capture?<br></div>Currently clang errors on all of them individually - is that the right behavior?<br>


<br>Also, how would you want me to start submitting patches for commit - do you want <br>me to break up the patch into smaller patches? - and if so, do you have any thoughts <br>on how I might want to break up the functionality per patch?</div>

</div></div></div></blockquote><div><br></div></div><div>Smaller patches are definitely better, if you can decompose the functionality into coherent smaller chunks. There are some hints on how to decompose the functionality here (but this division may or may not work for you):</div>

<div><br></div><div><a href="http://clang.llvm.org/docs/InternalsManual.html#how-to-add-an-expression-or-statement" target="_blank">http://clang.llvm.org/docs/InternalsManual.html#how-to-add-an-expression-or-statement</a></div>
</div>
</blockquote></div><br></div></div></div></div>