From f004e91c9b4171f04d606cc45b2fb76809ede143 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Mon, 12 May 2025 17:40:43 +0300 Subject: [PATCH] Return std::array type for boost::pfr::names_as_array(empty_struct{}) (fixes #195) --- include/boost/pfr/core_name.hpp | 3 +-- include/boost/pfr/detail/stdarray.hpp | 21 +++++---------------- test/core_name/run/fields_names.cpp | 8 ++++++++ 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/include/boost/pfr/core_name.hpp b/include/boost/pfr/core_name.hpp index 34cab9a..186d29b 100644 --- a/include/boost/pfr/core_name.hpp +++ b/include/boost/pfr/core_name.hpp @@ -83,8 +83,7 @@ auto names_as_array() noexcept { return detail::make_stdarray_from_tietuple( detail::tie_as_names_tuple(), - detail::make_index_sequence< tuple_size_v >(), - 1L + detail::make_index_sequence< tuple_size_v >() ); } diff --git a/include/boost/pfr/detail/stdarray.hpp b/include/boost/pfr/detail/stdarray.hpp index f17b2c4..0f802e4 100644 --- a/include/boost/pfr/detail/stdarray.hpp +++ b/include/boost/pfr/detail/stdarray.hpp @@ -12,29 +12,18 @@ #include #if !defined(BOOST_PFR_INTERFACE_UNIT) -#include // metaprogramming stuff #include -#include // for std::common_type_t -#include +#include +#include // metaprogramming stuff #endif namespace boost { namespace pfr { namespace detail { -template -constexpr auto make_stdarray(const Types&... t) noexcept { - return std::array, sizeof...(Types)>{t...}; -} - template -constexpr auto make_stdarray_from_tietuple(const T& t, std::index_sequence, int) noexcept { - return detail::make_stdarray( +constexpr auto make_stdarray_from_tietuple(const T& t, std::index_sequence) noexcept { + return std::array{ boost::pfr::detail::sequence_tuple::get(t)... - ); -} - -template -constexpr auto make_stdarray_from_tietuple(const T&, std::index_sequence<>, long) noexcept { - return std::array{}; + }; } }}} // namespace boost::pfr::detail diff --git a/test/core_name/run/fields_names.cpp b/test/core_name/run/fields_names.cpp index e04a44a..27212f2 100644 --- a/test/core_name/run/fields_names.cpp +++ b/test/core_name/run/fields_names.cpp @@ -131,6 +131,13 @@ void test_names_as_array_for_empty() { BOOST_TEST_EQ(value.empty(), true); } +void test_names_as_array_iteration_for_empty() { + const auto names = boost::pfr::names_as_array(); + for (std::string_view name : names) { + BOOST_ERROR("`names` must be empty"); + } +} + } // namespace testing int main() { @@ -141,6 +148,7 @@ int main() { testing::test_names_as_array(); testing::test_names_as_array_without_linkage(); testing::test_names_as_array_for_empty(); + testing::test_names_as_array_iteration_for_empty(); return boost::report_errors(); }