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
Either<string, int> myEither = left("hello"); // constructs a either containing a leftValue; | |
int count = myEither | |
.rightMap([](auto num) { return num + 1; }) // adds 1 if rightValue is present | |
.leftMap([](auto str) { return str + "world"; }) // appends "world" if leftValue is present | |
.leftMap([](auto str) { return str.size(); }) | |
.join(); // both sides have now the same type, lets join... |
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
// This... | |
float sqrt(float x) { | |
if (x < 0) { | |
throw std::string("x should be >= 0"); | |
} | |
return computeSqrt(x); | |
} | |
// ... can be transformed into this... | |
Either<std::string, float> sqrt(float x) { |
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
// And for the caller, the usage changes from this... | |
try { | |
float x = sqrt(-1); | |
std::cout << "sqrt(x) = " << x << std::endl; | |
} catch(std::string x) { | |
std::cout << "error occurred: " << x << std::endl; | |
} | |
// ... to this... | |
std::string msg = sqrt(-1) |
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
void* x = createInstance(); | |
int* y = new int(); | |
foo.bar(y); |
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
struct Widget { | |
Widget() { cout << "Widget created" << endl; } | |
~Widget() { cout << "Widget destroyed" << endl; } | |
}; | |
unique_ptr<Widget> createWindow() { | |
return make_unique<Widget>(); | |
} | |
int main() { |
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
struct Node { | |
string name; | |
weak_ptr<Node> parent; | |
vector<shared_ptr<Node>> children; | |
bool hasParent() { | |
return parent.expired(); | |
} | |
shared_ptr<Node> getParent() { |
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
struct Tree { | |
string const name; | |
value_ptr<Tree> left; | |
value_ptr<Tree> right; | |
Tree( | |
string const& name, | |
value_ptr<Tree> const& left = value_ptr<Tree>{}, | |
value_ptr<Tree> const& right = value_ptr<Tree>{}) | |
: name{name} |
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
struct Foo { | |
int bar() { | |
return ptr->bar(); | |
} | |
Foo(int x); | |
// Thanks to value_ptr we get value semantics for free | |
Foo(Foo const&) = default; | |
Foo& operator=(Foo const&) = default; |
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
struct Foo::Pimpl { | |
int x; | |
int bar() { return ++x; } | |
}; | |
Foo::Foo(int x) | |
: ptr{Foo::Pimpl{x}} | |
{} |