Skip to content

Commit 531db4d

Browse files
committed
Fixed lambda return type used in xfunction
1 parent 3b8e0c4 commit 531db4d

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

include/xtensor/core/xfunction.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ namespace xt
608608
// leading to warning about signed/unsigned conversions in the deeper layers of the access methods
609609

610610
return std::apply(
611-
[&](auto&... e)
611+
[&](auto&... e) -> const_reference
612612
{
613613
XTENSOR_TRY(check_index(shape(), args...));
614614
XTENSOR_CHECK_DIMENSION(shape(), args...);
@@ -631,7 +631,7 @@ namespace xt
631631
inline auto xfunction<F, CT...>::flat(size_type index) const -> const_reference
632632
{
633633
return std::apply(
634-
[&](auto&... e)
634+
[&](auto&... e) -> const_reference
635635
{
636636
return m_f(e.data_element(index)...);
637637
},
@@ -665,7 +665,7 @@ namespace xt
665665
// The static cast prevents the compiler from instantiating the template methods with signed integers,
666666
// leading to warning about signed/unsigned conversions in the deeper layers of the access methods
667667
return std::apply(
668-
[&](const auto&... e)
668+
[&](const auto&... e) -> const_reference
669669
{
670670
return m_f(e.unchecked(static_cast<size_type>(args)...)...);
671671
},
@@ -685,7 +685,7 @@ namespace xt
685685
inline auto xfunction<F, CT...>::element(It first, It last) const -> const_reference
686686
{
687687
return std::apply(
688-
[&](auto&... e)
688+
[&](auto&... e) -> const_reference
689689
{
690690
XTENSOR_TRY(check_element_index(shape(), first, last));
691691
return m_f(e.element(first, last)...);
@@ -826,7 +826,7 @@ namespace xt
826826
inline auto xfunction<F, CT...>::data_element(size_type i) const -> const_reference
827827
{
828828
return std::apply(
829-
[&](auto&... e)
829+
[&](auto&... e) -> const_reference
830830
{
831831
return m_f(e.data_element(i)...);
832832
},
@@ -957,7 +957,7 @@ namespace xt
957957
inline auto xfunction_iterator<F, CT...>::operator*() const -> reference
958958
{
959959
return std::apply(
960-
[&](auto&... it)
960+
[&](auto&... it) -> reference
961961
{
962962
return (p_f->m_f)(*it...);
963963
},
@@ -1109,7 +1109,7 @@ namespace xt
11091109
inline auto xfunction_stepper<F, CT...>::operator*() const -> reference
11101110
{
11111111
return std::apply(
1112-
[&](auto&... e)
1112+
[&](auto&... e) -> reference
11131113
{
11141114
return (p_f->m_f)(*e...);
11151115
},

test/test_xfunction.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "xtensor/containers/xarray.hpp"
1111
#include "xtensor/containers/xfixed.hpp"
1212
#include "xtensor/containers/xtensor.hpp"
13+
#include "xtensor/core/xvectorize.hpp"
1314
#include "xtensor/generators/xrandom.hpp"
1415
#include "xtensor/views/xview.hpp"
1516

@@ -193,6 +194,21 @@ namespace xt
193194
}
194195
}
195196

197+
TEST(xfunction, access_dangling_reference)
198+
{
199+
auto values_original = xt::xtensor<double, 1>{1., 2., 3.};
200+
auto indexes = xt::arange<size_t>(values_original.size());
201+
auto map = [&](const size_t& index) -> auto&
202+
{
203+
return values_original[index];
204+
};
205+
auto values_mapped = xt::vectorize(map)(indexes);
206+
for (size_t i = 0; i < values_original.size(); ++i)
207+
{
208+
EXPECT_TRUE(&(values_mapped[i]) == &(values_original[i]));
209+
}
210+
}
211+
196212
TEST(xfunction, unchecked)
197213
{
198214
xfunction_features f;

0 commit comments

Comments
 (0)