Last active
October 3, 2020 15:04
-
-
Save BruJu/d315b41e392d6f6a45102097843ab000 to your computer and use it in GitHub Desktop.
Optional Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <optional> | |
struct Contained { | |
Contained() { | |
std::cout << "Contained()\n"; | |
} | |
Contained(const Contained &) { | |
std::cout << "ContainedConstructor\n"; | |
} | |
Contained & operator=(const Contained &) { | |
std::cout << "ContainedAffect\n"; | |
return *this; | |
} | |
}; | |
struct Container { | |
std::optional<Contained> m_content = std::nullopt; | |
Container() { | |
std::cout << "Container()\n"; | |
}; | |
Container(const Contained & contained) { | |
m_content = contained; | |
std::cout << "ContainerConstructor\n"; | |
} | |
Container & operator=(const Contained & contained) { | |
m_content = contained; | |
std::cout << "ContainerAffect\n"; | |
return *this; | |
} | |
}; | |
int main() { | |
std::cout << "std::optional<Container> container = std::nullopt;\n"; | |
std::optional<Container> container = std::nullopt; | |
std::cout << "(1) container = Contained();\n"; | |
container = Contained(); | |
std::cout << "(2) container = Contained();\n"; | |
container = Contained(); | |
std::cout << "FILL FROM Optional<Contained>\n"; | |
std::optional<Contained> contained = std::nullopt; | |
container = contained; | |
if (container) return 1; | |
container = std::optional<Contained>(Contained()); | |
if (!container) return 2; | |
std::cout << "Ok\n"; | |
return 0; | |
} | |
// std::optional<Container> container = std::nullopt; | |
// (1) container = Contained(); | |
// ContainedConstructor | |
// ContainerConstructor | |
// (2) container = Contained(); | |
// ContainedAffect | |
// ContainerAffect | |
// | |
// -> std::optional use the optimal option to fill Container |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment