C++ помечает арифметическую жестко запрограммированную дробь как constexpr в однострочном виде ⇐ C++
C++ помечает арифметическую жестко запрограммированную дробь как constexpr в однострочном виде
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
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
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как использовать список в качестве входных данных в однострочном Python
Anonymous » » в форуме Python - 0 Ответы
- 10 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как использовать список в качестве входных данных в однострочном Python
Anonymous » » в форуме Python - 0 Ответы
- 5 Просмотры
-
Последнее сообщение Anonymous
-