From c5c251fdbf1af92567c8a6696dacdc6af8fe281d Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Tue, 24 Sep 2019 22:41:49 +0100 Subject: [PATCH] return copy instead of reference for scalar types (#218) --- include/boost/histogram/axis/category.hpp | 3 ++- test/axis_category_test.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/boost/histogram/axis/category.hpp b/include/boost/histogram/axis/category.hpp index 309aae0d..14a2bac3 100644 --- a/include/boost/histogram/axis/category.hpp +++ b/include/boost/histogram/axis/category.hpp @@ -128,7 +128,8 @@ public: /// Return value for index argument. /// Throws `std::out_of_range` if the index is out of bounds. - decltype(auto) value(index_type idx) const { + std::conditional_t::value, value_type, const value_type&> + value(index_type idx) const { if (idx < 0 || idx >= size()) BOOST_THROW_EXCEPTION(std::out_of_range("category index out of range")); return vec_meta_.first()[idx]; diff --git a/test/axis_category_test.cpp b/test/axis_category_test.cpp index 397554de..ed7c4f75 100644 --- a/test/axis_category_test.cpp +++ b/test/axis_category_test.cpp @@ -5,6 +5,7 @@ // or copy at http://www.boost.org/LICENSE_1_0.txt) #include +#include #include #include #include @@ -64,6 +65,15 @@ int main() { BOOST_TEST_NE(c, d); d = std::move(c); BOOST_TEST_EQ(d, a); + + enum class Foo { foo }; + + BOOST_TEST_TRAIT_SAME(decltype(std::declval>().value(0)), + const std::string&); + BOOST_TEST_TRAIT_SAME(decltype(std::declval>().value(0)), + const char*); + BOOST_TEST_TRAIT_SAME(decltype(std::declval>().value(0)), Foo); + BOOST_TEST_TRAIT_SAME(decltype(std::declval>().value(0)), int); } // axis::category with growth