Initial Course
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
add_course_exercise(NAME generic_lambdas MODULE 10_cpp14 STANDARD 14
|
||||
SOURCES src/generic_lambdas.cpp test/generic_lambdas_test.cpp)
|
||||
add_course_exercise(NAME auto_return MODULE 10_cpp14 STANDARD 14
|
||||
SOURCES src/auto_return.cpp test/auto_return_test.cpp)
|
||||
add_course_exercise(NAME make_unique MODULE 10_cpp14 STANDARD 14
|
||||
SOURCES src/make_unique.cpp test/make_unique_test.cpp)
|
||||
add_course_exercise(NAME digit_separators MODULE 10_cpp14 STANDARD 14
|
||||
SOURCES src/digit_separators.cpp test/digit_separators_test.cpp)
|
||||
@@ -0,0 +1,17 @@
|
||||
# Module 10: C++14 Improvements
|
||||
|
||||
## Learning Goals
|
||||
|
||||
- Write generic lambdas with `auto` parameters
|
||||
- Use return type deduction with `auto`
|
||||
- Prefer `std::make_unique` for heap ownership
|
||||
- Read numeric literals with digit separators
|
||||
|
||||
## Exercises
|
||||
|
||||
| Exercise | Feature |
|
||||
|----------|---------|
|
||||
| `generic_lambdas` | Generic lambda parameters |
|
||||
| `auto_return` | Decltype(auto) return deduction |
|
||||
| `make_unique` | Factory helpers |
|
||||
| `digit_separators` | Readable numeric literals |
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace cpp14 {
|
||||
|
||||
[[nodiscard]] inline auto copy_or_reference(const std::vector<int>& input) -> decltype(auto) {
|
||||
(void)input;
|
||||
static std::vector<int> empty;
|
||||
return empty;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline auto identity(int value) -> decltype(auto) { return value; }
|
||||
|
||||
} // namespace cpp14
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace cpp14 {
|
||||
|
||||
[[nodiscard]] auto million() -> int;
|
||||
[[nodiscard]] auto mask() -> std::uint32_t;
|
||||
[[nodiscard]] auto bits_set() -> int;
|
||||
|
||||
} // namespace cpp14
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace cpp14 {
|
||||
|
||||
[[nodiscard]] auto apply_twice(const std::vector<int>& values) -> std::vector<int>;
|
||||
[[nodiscard]] auto concat_any(const std::vector<std::string>& a, const std::vector<std::string>& b)
|
||||
-> std::vector<std::string>;
|
||||
|
||||
} // namespace cpp14
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace cpp14 {
|
||||
|
||||
struct Task {
|
||||
std::string name;
|
||||
int priority;
|
||||
};
|
||||
|
||||
[[nodiscard]] auto make_task(std::string name, int priority) -> std::unique_ptr<Task>;
|
||||
[[nodiscard]] auto clone_task(const Task& task) -> std::unique_ptr<Task>;
|
||||
[[nodiscard]] auto total_priority(const std::vector<std::unique_ptr<Task>>& tasks) -> int;
|
||||
|
||||
} // namespace cpp14
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "auto_return.hpp"
|
||||
|
||||
// Implementations live in the header because decltype(auto) requires a visible body.
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "digit_separators.hpp"
|
||||
|
||||
namespace cpp14 {
|
||||
|
||||
auto million() -> int {
|
||||
// TODO: Return 1'000'000 using digit separators
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto mask() -> std::uint32_t {
|
||||
// TODO: Return 0xFF00'00FF
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto bits_set() -> int {
|
||||
// TODO: Return population count of mask()
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace cpp14
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "generic_lambdas.hpp"
|
||||
|
||||
namespace cpp14 {
|
||||
|
||||
auto apply_twice(const std::vector<int>& values) -> std::vector<int> {
|
||||
(void)values;
|
||||
return {};
|
||||
}
|
||||
|
||||
auto concat_any(const std::vector<std::string>& a, const std::vector<std::string>& b)
|
||||
-> std::vector<std::string> {
|
||||
(void)a; (void)b;
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace cpp14
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "make_unique.hpp"
|
||||
|
||||
namespace cpp14 {
|
||||
|
||||
auto make_task(std::string name, int priority) -> std::unique_ptr<Task> {
|
||||
(void)name; (void)priority;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto clone_task(const Task& task) -> std::unique_ptr<Task> {
|
||||
(void)task;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto total_priority(const std::vector<std::unique_ptr<Task>>& tasks) -> int {
|
||||
(void)tasks;
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace cpp14
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "auto_return.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(AutoReturn, Identity) {
|
||||
EXPECT_EQ(cpp14::identity(42), 42);
|
||||
}
|
||||
|
||||
TEST(AutoReturn, CopyOrReference) {
|
||||
std::vector<int> data = {1, 2, 3};
|
||||
const auto& ref = cpp14::copy_or_reference(data);
|
||||
EXPECT_EQ(ref, data);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "digit_separators.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(DigitSeparators, Million) { EXPECT_EQ(cpp14::million(), 1'000'000); }
|
||||
TEST(DigitSeparators, Mask) { EXPECT_EQ(cpp14::mask(), 0xFF00'00FFU); }
|
||||
TEST(DigitSeparators, BitsSet) { EXPECT_EQ(cpp14::bits_set(), 16); }
|
||||
@@ -0,0 +1,10 @@
|
||||
#include "generic_lambdas.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(GenericLambdas, ApplyTwice) {
|
||||
EXPECT_EQ(cpp14::apply_twice({1, 2, 3}), (std::vector<int>{2, 4, 6}));
|
||||
}
|
||||
|
||||
TEST(GenericLambdas, ConcatAny) {
|
||||
EXPECT_EQ(cpp14::concat_any({"a"}, {"b", "c"}), (std::vector<std::string>{"a", "b", "c"}));
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "make_unique.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(MakeUnique, MakeTask) {
|
||||
auto task = cpp14::make_task("build", 10);
|
||||
ASSERT_NE(task, nullptr);
|
||||
EXPECT_EQ(task->name, "build");
|
||||
EXPECT_EQ(task->priority, 10);
|
||||
}
|
||||
|
||||
TEST(MakeUnique, TotalPriority) {
|
||||
std::vector<std::unique_ptr<cpp14::Task>> tasks;
|
||||
tasks.push_back(cpp14::make_task("a", 1));
|
||||
tasks.push_back(cpp14::make_task("b", 2));
|
||||
EXPECT_EQ(cpp14::total_priority(tasks), 3);
|
||||
}
|
||||
Reference in New Issue
Block a user