[cfe-commits] r148374 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/SemaCXX/constexpr-strlen.cpp

Matthieu Monrocq matthieu.monrocq at gmail.com
Thu Jan 19 12:37:11 PST 2012


Le 19 janvier 2012 20:58, Richard Smith <richard at metafoo.co.uk> a écrit :

> On Thu, January 19, 2012 16:52, Jonathan Sauer wrote:
> >>> Do you know if the same apply (I would guess so) to std::strlen ?
> >>>
> >>> It seems a pity that such a trivial function could not be constexpr as
> >>> making it constexpr is actually dead simple. I wonder if it would be
> >>> worth a Defect Report.
> >>>
> >> This would seem very reasonable to me...
> >
> > As well as a lot (all?) of the math functions in cmath. And clang's
> built-ins
> > (__builtin_sin etc), too.
>
> We can support __builtin_* in constant expressions as a conforming
> extension;
> this is already how we behave for all the __builtin_*s providing C library
> functions which we can constant-fold (namely, __builtin_strlen,
> __builtin_huge_val*, __builtin_inf*, __builtin_nan*, __builtin_fabs* and
> __builtin_copysign*).
>
> If you want to add more such builtins to this list, the first step is to
> implement the corresponding operation for llvm::APFloat. Support in clang
> should then be trivial to add -- it'd also be great to add constant
> folding to
> llvm for such cases.
>
> Constant-folding (non-__builtin_)strlen can theoretically break conforming
> code and is likely to lead to people accidentally writing non-portable
> code in
> practice, and I've not found a clause in C++11 allowing the implementation
> to
> mark more functions in the library as constexpr, so I'm reluctant to
> support
> it. If you want to be non-portable, use __builtin_strlen.
>
> - Richard
>
> Standard conformance was my main worry as well.

A `constexpr` strlen would allow:

  template <size_t N> struct Array;

to be instantiated with:

  Array<std::strlen("Hello, World!")> array;

While compilers which do not support it as `constexpr` would reject the
construct.

This is why I believe the matter should be settled in the standard:
constexpr is not just an optimization, it affects semantics.

-- Matthieu.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120119/6e74be4c/attachment.html>


More information about the cfe-commits mailing list