C++ Logo

STD-DISCUSSION

Advanced search

Subject: 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: 2019-11-15 14:58:42


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

STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups