C++ Logo


Advanced search

Re: How do I know whether a call to a constexpr standard library function is actually a constant expression?

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 15 Nov 2019 12:58:42 -0800
On sexta-feira, 15 de novembro de 2019 10:26:41 PST Brian Bi via Std-
Discussion wrote:
> There has been a push to declare as many standard library functions
> constexpr as possible. However, declaring a function constexpr does not
> guarantee that a call to that function will be a constant expression. If we
> don't know whether a call will be a constant expression, then the fact that
> it is marked constexpr is useless.
> In most cases, the standard does not specify the way in which standard
> library functions are implemented, as this is considered an implementation
> detail. However, in the case of a constexpr function, these implementation
> details actually become part of the function's interface, because they
> determine whether or not the function will be usable in a constant
> expression.
> Obviously the call cannot be a constant expression if the library function
> is specified to invoke some other function that is not constexpr (e.g.,
> std::sort invokes the user's comparator). However, it's not entirely clear
> to me what happens in all other cases.

Your only guarantee is that if you use that function in a constexpr context,
it'll be a constant expression. If you use in another context, then you can't
guarantee anything.

Why does it matter?

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel System Software Products

Received on 2019-11-15 15:01:03