-
Notifications
You must be signed in to change notification settings - Fork 1
/
boost_te.cpp
37 lines (32 loc) · 914 Bytes
/
boost_te.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include "boost/te.hpp" // Requires C++17 (Tested: GCC-6+, Clang-4.0+, Apple:Clang-9.1+)
namespace te = boost::te;
// Define interface of something which is drawable
struct Drawable
{
void draw(std::ostream &out) const
{
te::call([](auto const &self, auto &out) { self.draw(out); }, *this, out);
}
};
// Define implementation which is drawable (No inheritance)
struct Square
{
void draw(std::ostream &out) const { out << "Square"; }
};
// Define other implementation which is drawable (No inheritance)
struct Circle
{
void draw(std::ostream &out) const { out << "Circle"; }
};
// Define object which can hold drawable objects
void draw(te::poly<Drawable> const &drawable)
{
drawable.draw(std::cout);
}
int main()
{
// Call draw polymorphically (Value semantics / Small Buffer Optimization)
draw(Circle{}); // prints Circle
draw(Square{}); // prints Square
}