C++ Logo

sg14

Advanced search

Re: [SG14] Looking for expressions of interest in a more flexible 'constexpr if' statement

From: Tjernstrom, Staffan <Staffan.Tjernstrom_at_[hidden]>
Date: Thu, 8 Apr 2021 17:34:52 +0000
Interesting. I'm personally all in favour of anything that reduces branching overhead. After all, the moral equivalent of

if ( [[unlikely]] ( logging && max_log_level > message_log_level ) )
{
   do_the_logging();
}

is painfully present on most hot paths.
From: SG14 [mailto:sg14-bounces_at_[hidden]] On Behalf Of Matthew Bentley via SG14
Sent: Wednesday, 07 April, 2021 20:43
To: Low Latency:Game Dev/Financial/Trading/Simulation/Embedded Devices <sg14_at_[hidden]>
Cc: Matthew Bentley <mattreecebentley_at_[hidden]>
Subject: Re: [SG14] Looking for expressions of interest in a more flexible 'constexpr if' statement

Correction:
"A || B

If A is true, no runtime if statement occurs - C() does not need to be evaluated."

On Thu, 8 Apr 2021 at 12:39, Matthew Bentley <mattreecebentley_at_[hidden]<mailto:mattreecebentley_at_[hidden]>> wrote:
I often come across this situation:

if (A && B)
{
    C();
}
else
{
    D();
}



But when A is constexpr, there is no way to indicate to the if statement that B is not also constexpr. Ideally the compiler would detect this and evaluate A at compile-time anyway, getting rid of the unused code, but in my experience with various compilers this is not usually the case and cannot be relied upon. Further, even the simplest workaround for enabling use of constexpr is ugly:

if constexpr (A)
{
    if (B)
    {
        C();
    }
    else
    {
        D();
    }
}
else
{
    D();
}



I would support a more flexible usage of 'if constexpr' ala the constexpr function guidelines, which allow for case-dependent runtime or compile-time calling.
Very specifically, where A is constexpr and B is not:


A && B

If A is true, an if statement based on B is evaluated at runtime - C() must be evaluated.
If A is false, no runtime if statement occurs - C() does not need to be evaluated.


A || B

If A is true, no runtime if statement occurs - C() must be evaluated.
If A is false, an if statement based on B is evaluated at runtime - C() must be evaluated.


More complex statements like A && B || X are supersets of the above.

If a non-situation-dependent constexpr if situation is still useful for various contexts, it could follow the lead of constexpr functions and use the consteval keyword instead.


Further, this would also simplify the situational rules when const**** if statements rely on the calling of C++20 constexpr functions. Put simply, a 'consteval if' could only take consteval functions as parameters. A 'constexpr if' could take either constexpr or consteval functions as parameters.
M@

________________________________

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Received on 2021-04-08 12:34:57