Default Construction
Using std::optional with class constructors can be slightly inefficient sometimes. In this lesson, we'll learn a way to fix that.
We'll cover the following...
Movable Types
If you have a class with a default constructor, like:
Press + to interact
class UserName {public:UserName() : mName("Default"){}// ...};
How would you create an optional
that contains UserName{}
?
You can write:
Press + to interact
std::optional<UserName> u0; // empty optionalstd::optional<UserName> u1{}; // also empty// optional with default constructed object:std::optional<UserName> u2{UserName()};
That works but it creates an additional temporary object. If we traced each different constructor and ...