diff --git a/README.md b/README.md index cc77427..e10a8ff 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,18 @@ static_assert(check_5()); ``` +### Note on constexpr support + +Since `constexpr` requirements are actively changing, +you can use `beman::has_constexpr_support` to detect if our implementation +provide constexpr support for a specific specialization of `inplace_vector`. + +Note this is not part of the standard Library and should not be relied on once +`constexpr` requirement stabilize. + +Example Usage: +`static_assert(beman::has_constexpr_support>)`. + ## How to Build ### Compiler support diff --git a/include/beman/inplace_vector/inplace_vector.hpp b/include/beman/inplace_vector/inplace_vector.hpp index 0d7fc6a..320cfd1 100644 --- a/include/beman/inplace_vector/inplace_vector.hpp +++ b/include/beman/inplace_vector/inplace_vector.hpp @@ -306,6 +306,9 @@ concept container_compatible_range = std::ranges::input_range && std::convertible_to, T>; +template +concept satify_constexpr = N == 0 || std::is_trivial_v; + } // namespace beman::details::inplace_vector // Types implementing the `inplace_vector`'s storage @@ -425,13 +428,18 @@ template struct non_trivial { // Selects the vector storage. template using storage_for = std::conditional_t< - N == 0, zero_sized, - std::conditional_t, trivial, non_trivial>>; + !satify_constexpr, non_trivial, + std::conditional_t, trivial>>; } // namespace beman::details::inplace_vector::storage namespace beman { +template +concept has_constexpr_support = + details::inplace_vector::satify_constexpr; + /// Dynamically-resizable fixed-N vector with inplace storage. template struct inplace_vector diff --git a/tests/beman/inplace_vector/constexpr.test.cpp b/tests/beman/inplace_vector/constexpr.test.cpp index 74f16f2..2d44642 100644 --- a/tests/beman/inplace_vector/constexpr.test.cpp +++ b/tests/beman/inplace_vector/constexpr.test.cpp @@ -2,6 +2,10 @@ #include #include +// used for testing beman::has_constexpr_support +#include +#include + /** * These tests are only meant to test that suitable constexpr functions compiles * in a constant evaluation environment. @@ -25,6 +29,18 @@ struct Some { }; static_assert(std::is_trivial_v); +using beman::has_constexpr_support; +using beman::inplace_vector; + +static_assert(has_constexpr_support>); +static_assert(has_constexpr_support>); + +static_assert(has_constexpr_support>); +static_assert(!has_constexpr_support>); + +static_assert(has_constexpr_support, 0>>); +static_assert(!has_constexpr_support, 50>>); + #define TEST(NAME) \ static_assert(std::invoke([]() { \ NAME>(); \ @@ -268,6 +284,9 @@ struct Complex { }; static_assert(!std::is_trivially_default_constructible_v); +static_assert(has_constexpr_support>); +static_assert(!has_constexpr_support>); + #define TEST_EMPTY(NAME) \ static_assert(std::invoke([]() { \ NAME(); \