Skip to content

Commit 0d407ec

Browse files
authored
fix: make smart-pointer exercise easier (#934)
1 parent e6ba417 commit 0d407ec

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

exercises/concept/power-of-troy/.docs/instructions.md

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ The world of Troy is all about interaction.
6060
You want people to make trades by exchanging their possessions.
6161
6262
Write a function `exchange_artifacts` that returns nothing but takes two artifact smart-pointers to exchange the items.
63+
Remember, that you cannot copy a `unique_ptr`.
64+
This includes the usage in function parameters.
65+
Use a reference to the `unique_ptr` instead.
6366
6467
```cpp
6568
human uchiha{};

exercises/concept/power-of-troy/.docs/introduction.md

+43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
11
# Introduction
22

3+
## The `auto` Keyword in C++
4+
5+
In C++, the `auto` keyword is a powerful feature introduced in C++11, used to declare variables with an inferred data type.
6+
The compiler deduces the type of the variable based on its initializer, which can make code more readable and easier to maintain.
7+
8+
## Example Usage
9+
10+
Consider the following example where `auto` is used to declare variables:
11+
12+
```cpp
13+
auto dragon_population{3}; // dragon_population is deduced as an integer
14+
auto westeros{7.7777}; // westeros is deduced as a double
15+
auto wedding_location{"The Twins"}; // wedding_location is deduced as a const char*, not std::string
16+
```
17+
18+
In each case, the type of the variable is inferred from the value it is initialized with.
19+
20+
## Type Inference
21+
22+
The `auto` keyword helps by writing more concise and readable code by reducing the verbosity of explicit types.
23+
24+
```cpp
25+
const std::vector<std::string> pigeon_pie{"flour", "butter", "pigeon", "salt"};
26+
auto purple_wedding_pie{pigeon_pie};
27+
purple_wedding_pie.emplace_back("the strangler");
28+
```
29+
30+
In this loop, `auto` deduces the type of `purple_wedding_pie` as `std::vector<std::string>`, avoiding the need to explicitly specify the type again.
31+
32+
## Compatibility
33+
34+
The `auto` keyword is compatible with various C++ constructs making it a versatile tool in modern C++ programming.
35+
36+
```cpp
37+
auto& element{array[0]}; // reference to an element
38+
const auto object{otherObject}; // const type version of otherObject's type
39+
auto* ptr{&x}; // pointer to x with the same type as x, but as a pointer.
40+
```
41+
42+
In later concept we will often see the `auto` keyword with lambda expressions, range-based for-loops, and iterators.
43+
44+
## Smart Pointers
45+
346
Smart pointers are a modern C++ feature designed to provide automatic memory management, helping to prevent memory leaks and dangling pointers commonly associated with raw pointers.
447
They act as wrappers around raw pointers, adding additional functionality such as automatic memory deallocation when the pointer is no longer needed.
548

exercises/concept/power-of-troy/.meta/exemplar.h

+4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
namespace troy {
77

88
struct artifact {
9+
// constructors needed (until C++20)
10+
artifact(std::string name) : name(name) {}
911
std::string name;
1012
};
1113

1214
struct power {
15+
// constructors needed (until C++20)
16+
power(std::string effect) : effect(effect) {}
1317
std::string effect;
1418
};
1519

exercises/concept/power-of-troy/power_of_troy.h

+4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
namespace troy {
66

77
struct artifact {
8+
// constructors needed (until C++20)
9+
artifact(std::string name) : name(name) {}
810
std::string name;
911
};
1012

1113
struct power {
14+
// constructors needed (until C++20)
15+
power(std::string effect) : effect(effect) {}
1216
std::string effect;
1317
};
1418

0 commit comments

Comments
 (0)