diff --git a/modules/14_cpp26/include/constexpr_frontier.hpp b/modules/14_cpp26/include/constexpr_frontier.hpp index 7b33ebb..bbeec7f 100644 --- a/modules/14_cpp26/include/constexpr_frontier.hpp +++ b/modules/14_cpp26/include/constexpr_frontier.hpp @@ -1,12 +1,23 @@ #pragma once +#include #include -#include namespace cpp26 { -[[nodiscard]] auto compile_time_factorial(int n) -> long long; -[[nodiscard]] auto compile_time_sum(std::array values) -> int; -[[nodiscard]] auto is_sorted(std::array values) -> bool; +[[nodiscard]] constexpr auto compile_time_factorial(int n) -> long long { + auto result = 1LL; + for (auto i = 2; i <= n; ++i) { + result *= i; + } + return result; +} +[[nodiscard]] constexpr auto compile_time_sum(std::array values) -> int { + return std::ranges::fold_left(values, 0, std::plus{}); +} +[[nodiscard]] constexpr auto is_sorted(const std::array &values) -> bool { + return std::ranges::is_sorted(values); // also a constexpr function -} // namespace cpp26 +} + +} // namespace cpp26 \ No newline at end of file diff --git a/modules/14_cpp26/src/constexpr_frontier.cpp b/modules/14_cpp26/src/constexpr_frontier.cpp index d50c521..b8d7fd4 100644 --- a/modules/14_cpp26/src/constexpr_frontier.cpp +++ b/modules/14_cpp26/src/constexpr_frontier.cpp @@ -1,20 +1,6 @@ #include "constexpr_frontier.hpp" namespace cpp26 { - -auto compile_time_factorial(int n) -> long long { - (void)n; - return 1; -} - -auto compile_time_sum(std::array values) -> int { - (void)values; - return 0; -} - -auto is_sorted(std::array values) -> bool { - (void)values; - return false; -} + // constexpr functions should be defined in the header file } // namespace cpp26