From b11f8ca9a379d3284a6bb8bf1ab690c3754d417b Mon Sep 17 00:00:00 2001 From: Asa Hammond Date: Tue, 20 Feb 2024 19:45:11 -0800 Subject: [PATCH] constexpr only works on literal types --- test/util.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/test/util.cpp b/test/util.cpp index baa68bb6..dbfca1fa 100644 --- a/test/util.cpp +++ b/test/util.cpp @@ -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; +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);