You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 10-templates-exceptions-lambdas-smart-pointers.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -123,10 +123,10 @@ public:
123
123
}
124
124
};
125
125
126
-
auto o1 = Opt{ 1.2 };
127
-
auto o2 = Opt{ 3 };
128
-
auto o3 = Opt{};
129
-
auto o4 = Opt<size_t>{};
126
+
auto o1 = Opt{ 1.2 }; // T = double, valid = true
127
+
auto o2 = Opt{ 3 }; // T = int, valid = true
128
+
auto o3 = Opt{}; // T = char, valid = false
129
+
auto o4 = Opt<size_t>{}; // T = size_t, valid = false
130
130
```
131
131
132
132
Some things to note about this program:
@@ -139,7 +139,7 @@ Some things to note about this program:
139
139
140
140
* Calling member function `hasValue()` is always safe, yielding a `bool`. Calling `get()` on an `Opt` with no value immediately terminates the program (the keyword `throw` is explained later in this Chapter).
141
141
142
-
Of course, this simple class is of limited practical use, if you need a type to be considered optionally valid without using a "special" value to indicate this, then make use of `std::optional<T>` from the Standard Library.
142
+
Of course, this simple class is of limited practical use; if you need a type to be considered optionally valid without using a "special" value to indicate this, then making use of `std::optional<T>` from the Standard Library is recommended.
143
143
144
144
Member functions can be template functions, too. The following program defines a `Stringy` class with a `std::string` member, which can be initialized from another `std::string`, a `std::string_view` or a `const char *`:
145
145
@@ -854,7 +854,7 @@ int main() {
854
854
cout << "Name not recognized!\n";
855
855
}
856
856
}
857
-
}
857
+
}
858
858
```
859
859
860
860
This is one of the larger programs we have seen, and covers much of the contents of this Chapter:
@@ -885,4 +885,4 @@ This is one of the larger programs we have seen, and covers much of the contents
885
885
886
886
* Change `Pupil` and `Class` to be `class`es instead of `struct`s, with `private:` data members. Hint: you will need to write some `public:` getters.
0 commit comments