Last active
April 2, 2025 22:48
-
-
Save uliwitness/931873c0b2edaa537b905418a024be04 to your computer and use it in GitHub Desktop.
This file contains hidden or 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_shared_ptr.hpp" | |
| class Bar { | |
| public: | |
| explicit Bar(int n) : _num(n) {} | |
| void print() { std::cout << "Bar::_num = " << _num << std::endl; } | |
| int _num; | |
| }; | |
| class Foo { | |
| public: | |
| Foo(const nonoptional_shared_ptr<Bar>& p) : _neverNull(p) {} | |
| void print() { _neverNull->print(); } | |
| protected: | |
| nonoptional_shared_ptr<Bar> _neverNull; | |
| }; | |
| int main(int argc, const char** argv) { | |
| std::shared_ptr<Bar> bar = std::make_shared<Bar>(42); | |
| std::shared_ptr<Foo> dest; | |
| if_nonnullptr<Bar>(bar, | |
| [&dest](const nonoptional_shared_ptr<Bar>& p) { | |
| dest = std::make_shared<Foo>(p); | |
| }, | |
| [](){ | |
| std::cerr << "Error!" << std::endl; | |
| }); | |
| dest->print(); | |
| nonoptional_shared_ptr<Foo> dest2 = make_nonoptional_shared<Foo>(make_nonoptional_shared<Bar>(55)); | |
| dest2->print(); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment