Solved: 43 - Concepts
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <concepts>
|
#include <concepts>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -10,14 +11,13 @@ namespace cpp20 {
|
|||||||
|
|
||||||
template <std::integral T>
|
template <std::integral T>
|
||||||
[[nodiscard]] auto double_value(T value) -> T {
|
[[nodiscard]] auto double_value(T value) -> T {
|
||||||
(void)value;
|
return value * 2;
|
||||||
return T{};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <std::floating_point T>
|
template <std::floating_point T>
|
||||||
[[nodiscard]] auto clamp01(T value) -> T {
|
[[nodiscard]] auto clamp01(T value) -> T {
|
||||||
(void)value;
|
// Converts the 0 and 1 to the right type, which fulfills the std::floating_point concept
|
||||||
return T{};
|
return std::clamp(value, T{0}, T{1});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -28,10 +28,17 @@ concept StringLike = requires(T value) {
|
|||||||
|
|
||||||
template <StringLike T>
|
template <StringLike T>
|
||||||
[[nodiscard]] auto uppercase_copy(const T& text) -> std::string {
|
[[nodiscard]] auto uppercase_copy(const T& text) -> std::string {
|
||||||
(void)text;
|
auto oss = std::ostringstream{};
|
||||||
return {};
|
for (auto idx = 0uz; idx < text.size(); idx++) {
|
||||||
|
oss << static_cast<char>(std::toupper(text.data()[idx]));
|
||||||
|
}
|
||||||
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] auto sum_integral(const std::vector<int>& data) -> int;
|
template<typename T>
|
||||||
|
requires std::integral<T>
|
||||||
|
[[nodiscard]] auto sum_integral(const std::vector<T>& data) -> T {
|
||||||
|
return std::ranges::fold_left(data, T{0}, std::plus{});
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace cpp20
|
} // namespace cpp20
|
||||||
|
|||||||
@@ -2,9 +2,5 @@
|
|||||||
|
|
||||||
namespace cpp20 {
|
namespace cpp20 {
|
||||||
|
|
||||||
auto sum_integral(const std::vector<int>& data) -> int {
|
|
||||||
(void)data;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace cpp20
|
} // namespace cpp20
|
||||||
|
|||||||
@@ -15,5 +15,5 @@ TEST(Concepts, UppercaseCopy) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(Concepts, SumIntegral) {
|
TEST(Concepts, SumIntegral) {
|
||||||
EXPECT_EQ(cpp20::sum_integral({1, 2, 3}), 6);
|
EXPECT_EQ(cpp20::sum_integral<int>({1, 2, 3}), 6);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user