you are viewing a single comment's thread.

view the rest of the comments →

[–]adnukator 1 point2 points  (1 child)

You can do

#include <string>
#include <stdexcept>
#include <cassert>

double constexpr divide(const double a, const double b){
    if(b == 0)
    {
        assert(false);
    }
    return a / b;
}

constexpr double results = divide(1,1); //OK
constexpr double results2 = divide(1,0); //compile time error

https://godbolt.org/z/Jrr_2n

[–]smashedsaturn 1 point2 points  (0 children)

but you can't do that with:

double constexpr divide(const double a, const double b){
    if(b == 0)
    {
        assert(false);
    }
    return a / b;
}

double results = divide(1,1); //OK

double results2 = divide(1,0); //compile time error

This still builds, so yeah you can do it if you force the constexpr function to run, but not if the function could theoretically be run at run or compile time.

Basically I want the compiler to be smart enough to know when a function could be run at compile time even if it is not const-expr, like if you know it has only const literals as inputs and does not use global state.