Solved: 50 - Constexpr Frontier
This commit is contained in:
@@ -1,12 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
|
||||
namespace cpp26 {
|
||||
|
||||
[[nodiscard]] auto compile_time_factorial(int n) -> long long;
|
||||
[[nodiscard]] auto compile_time_sum(std::array<int, 5> values) -> int;
|
||||
[[nodiscard]] auto is_sorted(std::array<int, 5> 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<int, 5> values) -> int {
|
||||
return std::ranges::fold_left(values, 0, std::plus{});
|
||||
}
|
||||
[[nodiscard]] constexpr auto is_sorted(const std::array<int, 5> &values) -> bool {
|
||||
return std::ranges::is_sorted(values); // also a constexpr function
|
||||
|
||||
}
|
||||
|
||||
} // namespace cpp26
|
||||
@@ -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<int, 5> values) -> int {
|
||||
(void)values;
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto is_sorted(std::array<int, 5> values) -> bool {
|
||||
(void)values;
|
||||
return false;
|
||||
}
|
||||
// constexpr functions should be defined in the header file
|
||||
|
||||
} // namespace cpp26
|
||||
|
||||
Reference in New Issue
Block a user