Skip to content

Commit

Permalink
Merge pull request #215 from arximboldi/box-recursive-type
Browse files Browse the repository at this point in the history
Box recursive type
  • Loading branch information
arximboldi authored Aug 22, 2024
2 parents af857bd + abf10a4 commit 6541206
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions test/box/recursive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <immer/set.hpp>
#include <immer/vector.hpp>

#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <catch.hpp>

#include <catch2/catch_test_macros.hpp>

struct rec_vec
Expand Down Expand Up @@ -106,3 +110,80 @@ TEST_CASE("recursive set")
CHECK(v3.children.count(rec_set{13, {}}) == 1);
CHECK(v3.children.count(rec_set{14, {}}) == 0);
}

namespace example1 {

struct a_type;

struct b_type
{
b_type(int a, immer::box<a_type> val);
int a;
immer::box<a_type> val;
};

struct a_type
{
a_type();
// this does not work with std::optional, because it seems like
// `immer::box<b_type>` is still not considered complete at this point...
boost::optional<immer::box<b_type>> b;
};

b_type::b_type(int a, immer::box<a_type> val)
: a(a)
, val(val)
{}

a_type::a_type()
: b{}
{}

} // namespace example1

TEST_CASE("recursive optional")
{
auto x = example1::a_type{};
auto y = example1::b_type{42, x};
CHECK(y.a == 42);
}

namespace example2 {

struct empty_t
{};

struct a_type;

struct b_type
{
b_type(int a, immer::box<a_type> val);
int a;
immer::box<a_type> val;
};

struct a_type
{
a_type();
// this does not work with std::variant, because it seems like
// `immer::box<b_type>` is still not considered complete at this point...
boost::variant<empty_t, b_type> b;
};

b_type::b_type(int a, immer::box<a_type> val)
: a(a)
, val(val)
{}

a_type::a_type()
: b{}
{}

} // namespace example2

TEST_CASE("recursive variant")
{
auto x = example2::a_type{};
auto y = example2::b_type{42, x};
CHECK(y.a == 42);
}

0 comments on commit 6541206

Please sign in to comment.