C++ Logo

std-discussion

Advanced search

Re: 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 15:00:44 -0600
On Fri, Nov 15, 2019 at 2:58 PM Thiago Macieira via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> 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.
>

What do you mean by "constexpr context"?


>
> Why does it matter?
>

It matters because it determines whether:

    constexpr auto x = some_constexpr_library_function(some_args)

actually compiles.


>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Software Architect - Intel System Software Products
>
>
>
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>


-- 
*Brian Bi*

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