C++ Logo


Advanced search

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

From: Brian Bi <bbi5291_at_[hidden]>
Date: Fri, 15 Nov 2019 12:26:41 -0600
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

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.
*Brian Bi*

Received on 2019-11-15 12:29:12