<br><br><div class="gmail_quote">Le 19 janvier 2012 20:58, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>></span> a écrit :<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Thu, January 19, 2012 16:52, Jonathan Sauer wrote:<br>
>>> Do you know if the same apply (I would guess so) to std::strlen ?<br>
>>><br>
>>> It seems a pity that such a trivial function could not be constexpr as<br>
>>> making it constexpr is actually dead simple. I wonder if it would be<br>
>>> worth a Defect Report.<br>
>>><br>
>> This would seem very reasonable to me...<br>
><br>
> As well as a lot (all?) of the math functions in cmath. And clang's built-ins<br>
> (__builtin_sin etc), too.<br>
<br>
</div>We can support __builtin_* in constant expressions as a conforming extension;<br>
this is already how we behave for all the __builtin_*s providing C library<br>
functions which we can constant-fold (namely, __builtin_strlen,<br>
__builtin_huge_val*, __builtin_inf*, __builtin_nan*, __builtin_fabs* and<br>
__builtin_copysign*).<br>
<br>
If you want to add more such builtins to this list, the first step is to<br>
implement the corresponding operation for llvm::APFloat. Support in clang<br>
should then be trivial to add -- it'd also be great to add constant folding to<br>
llvm for such cases.<br>
<br>
Constant-folding (non-__builtin_)strlen can theoretically break conforming<br>
code and is likely to lead to people accidentally writing non-portable code in<br>
practice, and I've not found a clause in C++11 allowing the implementation to<br>
mark more functions in the library as constexpr, so I'm reluctant to support<br>
it. If you want to be non-portable, use __builtin_strlen.<br>
<br>
- Richard<br>
<br></blockquote><div>Standard conformance was my main worry as well.<br><br>A `constexpr` strlen would allow:<br><br>  template <size_t N> struct Array;<br><br>to be instantiated with:<br><br>  Array<std::strlen("Hello, World!")> array;<br>
<br>While compilers which do not support it as `constexpr` would reject the construct.<br><br>This is why I believe the matter should be settled in the standard: constexpr is not just an optimization, it affects semantics.<br>
<br>-- Matthieu. <br></div></div>