Solved: 50 - Constexpr Frontier
This commit is contained in:
@@ -1,12 +1,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstddef>
|
|
||||||
|
|
||||||
namespace cpp26 {
|
namespace cpp26 {
|
||||||
|
|
||||||
[[nodiscard]] auto compile_time_factorial(int n) -> long long;
|
[[nodiscard]] constexpr auto compile_time_factorial(int n) -> long long {
|
||||||
[[nodiscard]] auto compile_time_sum(std::array<int, 5> values) -> int;
|
auto result = 1LL;
|
||||||
[[nodiscard]] auto is_sorted(std::array<int, 5> values) -> bool;
|
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
|
}
|
||||||
|
|
||||||
|
} // namespace cpp26
|
||||||
@@ -1,20 +1,6 @@
|
|||||||
#include "constexpr_frontier.hpp"
|
#include "constexpr_frontier.hpp"
|
||||||
|
|
||||||
namespace cpp26 {
|
namespace cpp26 {
|
||||||
|
// constexpr functions should be defined in the header file
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace cpp26
|
} // namespace cpp26
|
||||||
|
|||||||
Reference in New Issue
Block a user