From a938086e416639152f8da486ab21b0731cf05700 Mon Sep 17 00:00:00 2001 From: Loic Gouarin Date: Thu, 29 Oct 2026 18:56:52 +0100 Subject: [PATCH] Return a value from xoptional_assembly_storage::data() Both data() overloads had the statement pointer(front()); with no return. C++ parses that line as the declaration of a local function named front, which is what GCC's -Wparentheses and -Wshadow pointed at. Each overload then fell off the end of a function returning a value: - calling one overload is undefined behavior. With GCC 15 at -O2, stor.data()->value() segfaults. - calling both in one translation unit does not compile, because the two local declarations of xt::front have different return types. Return the closure pointer to the front element, and add a test for the mutable and const overloads. --- .../optional/xoptional_assembly_storage.hpp | 4 ++-- test/test_xoptional_assembly_storage.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/xtensor/optional/xoptional_assembly_storage.hpp b/include/xtensor/optional/xoptional_assembly_storage.hpp index 0ec1c0a44..041de952a 100644 --- a/include/xtensor/optional/xoptional_assembly_storage.hpp +++ b/include/xtensor/optional/xoptional_assembly_storage.hpp @@ -320,13 +320,13 @@ namespace xt template inline auto xoptional_assembly_storage::data() noexcept -> pointer { - pointer(front()); + return pointer(front()); } template inline auto xoptional_assembly_storage::data() const noexcept -> const_pointer { - const_pointer(front()); + return const_pointer(front()); } template diff --git a/test/test_xoptional_assembly_storage.cpp b/test/test_xoptional_assembly_storage.cpp index 60a44752f..b79bc22c3 100644 --- a/test/test_xoptional_assembly_storage.cpp +++ b/test/test_xoptional_assembly_storage.cpp @@ -84,6 +84,24 @@ namespace xt ASSERT_EQ(stor[3], stor.back()); } + TEST(xoptional_assembly_storage, data) + { + storage_type v = {56, 2, 3, 5}; + flag_storage_type f = {false, true, true, true}; + auto stor = optional_assembly_storage(v, f); + + auto ptr = stor.data(); + ASSERT_EQ(*ptr, stor.front()); + ptr->value() = 12; + ptr->has_value() = true; + ASSERT_EQ(stor[0], 12); + + const auto& cstor = stor; + auto cptr = cstor.data(); + ASSERT_EQ(*cptr, cstor.front()); + ASSERT_EQ(cptr->value(), 12); + } + TEST(xoptional_assembly_storage, iterator) { storage_type v = {56, 2, 3, 5};