Skip to content

Commit

Permalink
constexpr only works on literal types
Browse files Browse the repository at this point in the history
  • Loading branch information
asa committed Feb 21, 2024
1 parent ed3c184 commit b11f8ca
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion test/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,25 @@ TEST_CASE("match deriv")
CHECK(y == 42);
}

// only literal types can be used in a constexpr

using literal_variant_t = std::variant<int, float, double>;
struct literal_deriv_t : literal_variant_t
{
using literal_variant_t::literal_variant_t;
};

TEST_CASE("match constexpr")
{
constexpr auto v = literal_variant_t{42};
constexpr auto y =
lager::match(v)([](int x) { return x; }, [](auto) { return 0; });
CHECK(y == 42);
}

TEST_CASE("match deriv constexpr ")
{
constexpr auto v = deriv_t{42};
constexpr auto v = literal_deriv_t{42};
constexpr auto y =
lager::match(v)([](int x) { return x; }, [](auto) { return 0; });
CHECK(y == 42);
Expand Down

0 comments on commit b11f8ca

Please sign in to comment.