I have a simple function that checks if a value is bigger than one third:
bool check(float x) {
return x > 1.f/3;
}
Based on what I've read, the compilers will probably calculate the result of 1.f/3 at compile time to improve performance, but is not required to. To force him doing that, it's recommended to use:
bool check(float x) {
constexpr float one_third = 1.f/3;
return x > one_third;
}
My question is whether there is a way to do this as a one-liner, without having to define a separate expression. Something like:
bool check(float x) {
return x > (constexpr float(1.f/3));
}
I could not find any way to do it and wanted to ask for confirmation. I found if-constexpr, but that requires both sides of the if to be constexpr, not just an internal subset of it. Lambda-constexpr or consteval could maybe work as oneliners?
constevallambda (C++20) might be used:but I prefer the
static constexprvariant for readability