Skip to content

Commit da786e4

Browse files
authored
Return a value from xoptional_assembly_storage::data() (#2955)
# Checklist - [x] The title and commit message(s) are descriptive. - [x] Small commits made to fix your PR have been squashed to avoid history pollution. - [x] Tests have been added for new features or bug fixes. - [x] API of new functions and classes are documented (no public API changes). # Description Both overloads of `xoptional_assembly_storage::data()` contained `pointer(front());` without `return`. C++ parses that statement as the declaration of a local function named `front`, so the functions fall off the end without returning a value: ```cpp auto stor = xt::optional_assembly_storage(v, f); stor.data()->value(); // undefined behavior, segfaults with GCC 15 at -O2 ``` Calling both overloads in the same translation unit does not compile either, because the two local declarations of `front` have different return types. GCC reports the statement with `-Wparentheses` and `-Wshadow`. `data()` now returns the closure pointer to the front element. `xoptional_assembly_storage.data` checks the mutable and const overloads; it does not compile on master and passes with the fix.
1 parent 75abad5 commit da786e4

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎include/xtensor/optional/xoptional_assembly_storage.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,13 @@ namespace xt
320320
template <class VE, class FE>
321321
inline auto xoptional_assembly_storage<VE, FE>::data() noexcept -> pointer
322322
{
323-
pointer(front());
323+
return pointer(front());
324324
}
325325

326326
template <class VE, class FE>
327327
inline auto xoptional_assembly_storage<VE, FE>::data() const noexcept -> const_pointer
328328
{
329-
const_pointer(front());
329+
return const_pointer(front());
330330
}
331331

332332
template <class VE, class FE>

‎test/test_xoptional_assembly_storage.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ namespace xt
8484
ASSERT_EQ(stor[3], stor.back());
8585
}
8686

87+
TEST(xoptional_assembly_storage, data)
88+
{
89+
storage_type v = {56, 2, 3, 5};
90+
flag_storage_type f = {false, true, true, true};
91+
auto stor = optional_assembly_storage(v, f);
92+
93+
auto ptr = stor.data();
94+
ASSERT_EQ(*ptr, stor.front());
95+
ptr->value() = 12;
96+
ptr->has_value() = true;
97+
ASSERT_EQ(stor[0], 12);
98+
99+
const auto& cstor = stor;
100+
auto cptr = cstor.data();
101+
ASSERT_EQ(*cptr, cstor.front());
102+
ASSERT_EQ(cptr->value(), 12);
103+
}
104+
87105
TEST(xoptional_assembly_storage, iterator)
88106
{
89107
storage_type v = {56, 2, 3, 5};

0 commit comments

Comments
 (0)