diff --git a/include/boost/pfr/detail/fields_count.hpp b/include/boost/pfr/detail/fields_count.hpp index 625159a..878d64f 100644 --- a/include/boost/pfr/detail/fields_count.hpp +++ b/include/boost/pfr/detail/fields_count.hpp @@ -221,7 +221,7 @@ constexpr std::size_t fields_count_compiler_limitation_next(std::size_t n) noexc ///////////////////// Fields count upper bound based on sizeof(T) template constexpr std::size_t fields_count_upper_bound_loose() noexcept { - return sizeof(T) * std::numeric_limits::digits; + return sizeof(T) * std::numeric_limits::digits + 1 /* +1 for "Arrays of Length Zero" extension */; } ///////////////////// Fields count binary search. diff --git a/test/core/run/zero_size_array.cpp b/test/core/run/zero_size_array.cpp new file mode 100644 index 0000000..a258d5e --- /dev/null +++ b/test/core/run/zero_size_array.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2025-2025 Antony Polukhin +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test that PFR works with GCCs "Arrays of Length Zero" extension + +#include + +#include + +#if defined(__GNUC__) && (BOOST_PFR_USE_LOOPHOLE || BOOST_PFR_USE_CPP17) +struct zero_array { + char data[0]; +}; + +struct aggr { + zero_array a; +}; + +static_assert(sizeof(zero_array) == 0); +static_assert(sizeof(aggr) == 0); + +int main() { + aggr a; + const auto result = boost::pfr::structure_tie(a); + static_assert(std::tuple_size::value == 1); + return sizeof(std::get<0>(result).data); +} +#else +int main() {} +#endif +