From 0394ea1feda98e5e28be57b9b4efbdb9e4dbc400 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 27 Mar 2023 13:36:46 -0400 Subject: [PATCH 1/2] Compile-time evaluation --- README.md | 21 +++++++++++++++++++++ tests/example_test.cpp | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/README.md b/README.md index 3e53871..2cc5626 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,27 @@ We support Visual Studio, macOS, Linux, freeBSD. We support big and little endia We assume that the rounding mode is set to nearest (`std::fegetround() == FE_TONEAREST`). +## C++20: compile-time evaluation (constexpr) + +In C++20, you may use `fast_float::from_chars` to parse strings +at compile-time, as in the following example: + +```C++ +// consteval forces compile-time evaluation of the function in C++20. +consteval double parse(std::string_view input) { + double result; + auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result); + if(answer.ec != std::errc()) { return -1.0; } + return result; +} + +// This function should compile to a function which +// merely returns 3.1415. +double constexptest() { + return parse("3.1415 input"); +} +``` + ## Using commas as decimal separator diff --git a/tests/example_test.cpp b/tests/example_test.cpp index f2a0bce..cb9be38 100644 --- a/tests/example_test.cpp +++ b/tests/example_test.cpp @@ -43,6 +43,22 @@ void many_loop() { } } +#if FASTFLOAT_IS_CONSTEXPR +// consteval forces compile-time evaluation of the function in C++20. +consteval double parse(std::string_view input) { + double result; + auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result); + if(answer.ec != std::errc()) { return -1.0; } + return result; +} + +// This function should compile to a function which +// merely returns 3.1415. +double constexptest() { + return parse("3.1415 input"); +} +#endif + int main() { const std::string input = "3.1416 xyz "; double result; @@ -55,5 +71,10 @@ int main() { return EXIT_FAILURE; } many_loop(); +#if FASTFLOAT_IS_CONSTEXPR + if constexpr(constexptest() != 3.1415) { + return EXIT_FAILURE; + } +#endif return EXIT_SUCCESS; } From d7ba016c736a2812c9c742e3119b55f83ec00131 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 27 Mar 2023 13:46:51 -0400 Subject: [PATCH 2/2] Fix. --- README.md | 2 +- tests/example_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2cc5626..63c2c8c 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ consteval double parse(std::string_view input) { // This function should compile to a function which // merely returns 3.1415. -double constexptest() { +constexpr double constexptest() { return parse("3.1415 input"); } ``` diff --git a/tests/example_test.cpp b/tests/example_test.cpp index cb9be38..91437de 100644 --- a/tests/example_test.cpp +++ b/tests/example_test.cpp @@ -54,7 +54,7 @@ consteval double parse(std::string_view input) { // This function should compile to a function which // merely returns 3.1415. -double constexptest() { +constexpr double constexptest() { return parse("3.1415 input"); } #endif