C++ помечает арифметическую жестко запрограммированную дробь как constexpr в однострочном видеC++

Программы на C++. Форум разработчиков
Anonymous
C++ помечает арифметическую жестко запрограммированную дробь как constexpr в однострочном виде

Сообщение Anonymous »


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?


Источник: https://stackoverflow.com/questions/781 ... -one-liner

Вернуться в «C++»