File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed
Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 1+ cmake_minimum_required (VERSION 3.30.0 )
2+
3+ project (main)
4+
5+ set (CMAKE_CXX_STANDARD 26)
6+ set (CMAKE_CXX_COMPILER clang++)
7+ set (CMAKE_EXPORT_COMPILE_COMMANDS ON )
8+
9+ set (SOURCES
10+ main.cpp
11+ )
12+
13+ include_directories (
14+ /usr/local/include
15+ /opt/homebrew/include
16+ ${CMAKE_CURRENT_SOURCE_DIR}
17+ )
18+
19+ link_directories (
20+ /usr/local/lib
21+ /opt/homebrew/lib
22+ )
23+
24+ add_executable (main
25+ ${SOURCES}
26+ )
Original file line number Diff line number Diff line change 1+ #include < boost/variant2/variant.hpp>
2+ #include < vector>
3+ #include < print>
4+
5+ using namespace std ;
6+ using namespace boost ::variant2;
7+
8+ struct Recangle
9+ {
10+ double area () const { return width * height; }
11+
12+ int width;
13+ int height;
14+ };
15+
16+ struct Circle
17+ {
18+ double area () const { return 3.14 * radius * radius; }
19+ int radius;
20+ };
21+
22+ double total_area (const vector<variant<Recangle, Circle>>& shapes)
23+ {
24+ double total = 0 ;
25+ for (const auto & shape : shapes)
26+ total += visit ([](auto const & s) { return s.area (); }, shape);
27+
28+ return total;
29+ }
30+
31+ int main (int argc, char * argv[])
32+ {
33+ vector<variant<Recangle, Circle>> shapes;
34+
35+ shapes.push_back (Recangle{5 , 10 });
36+ shapes.push_back (Circle{3 });
37+
38+ println (" Total area: {}" , total_area (shapes));
39+
40+ return 0 ;
41+ }
You can’t perform that action at this time.
0 commit comments