On Fri, Oct 19, 2012 at 10:04 PM, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk" target="_blank" class="cremed">richard@metafoo.co.uk</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div>[Crossposted to both GCC and Clang dev lists]</div><div>
<br></div>Hi,<div><br></div><div>One issue facing library authors wanting to use C++11's constexpr feature is that the same implementation must be provided for both the case of function invocation substitution and for execution at runtime. Due to the constraints on constexpr function definitions, this can force an implementation of a library function to be inefficient. To counteract this, I'd like to propose the addition of a builtin:</div>
<div><br></div><div> bool __builtin_constexpr_p()</div><div><br></div><div>This builtin would only be supported within constexpr function definitions. If the containing function is undergoing function invocation substitution, it returns true. Otherwise, it returns false. Hence we can implement library functions with a pattern like:</div>
<div><br></div><div><div> constexpr int constexpr_strncmp(const char *p, const char *q, size_t n) {</div><div> return !n ? 0 : *p != *q ? *p - *q : !*p ? 0 : constexpr_strncmp(p+1, q+1, n-1);</div><div> }</div></div>
<div> __attribute__((always_inline)) constexpr int my_strncmp(const char *p, const char *q, size_t n) {</div><div> return __builtin_constexpr_p() ? constexpr_strncmp(p, q, n) : strncmp(p, q, n);</div><div> }</div><div>
<br></div><div>Does this seem reasonable?</div></blockquote><div><br></div><div>Yes, especially the primary functionality. However, I have some concerns about the interface. Let me hypothesize a different interface:</div>
<div><br></div><div>This stays the same...</div><div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
constexpr int constexpr_strncmp(const char *p, const char *q, size_t n) {<br> return !n ? 0 : *p != *q ? *p - *q : !*p ? 0 : constexpr_strncmp(p+1, q+1, n-1);<br>}</blockquote></div><div><br></div><div>But here we do something different on the actual declaration:</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">[[constexpr_alias(constexpr_strncmp)]]<br>int strncmp(const char *p, const char *q, size_t n);</blockquote>
<div><br></div></div><div>When parsing the *declaration* of this function, we lookup the function name passed to constexpr_alias. We must find a constexpr function with an identical signature. Then, at function invocation substitution of strncmp, we instead substitute the body of constexpr_strncmp.</div>
<div><br></div><div>This seems more direct (no redirection in the code), and it also provides a specific advantage of allowing this to be easily added to an existing declaration in a declaration-only header file without impacting or changing the name of the runtime executed body or definition.</div>
</div></div>