return copy instead of reference for scalar types (#218)

This commit is contained in:
Hans Dembinski 2019-09-24 22:41:49 +01:00 committed by GitHub
parent d0a1c027ee
commit c5c251fdbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -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<std::is_scalar<value_type>::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];

View File

@ -5,6 +5,7 @@
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/histogram/axis/category.hpp>
#include <boost/histogram/axis/ostream.hpp>
#include <boost/histogram/detail/cat.hpp>
@ -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<axis::category<std::string>>().value(0)),
const std::string&);
BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<const char*>>().value(0)),
const char*);
BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<Foo>>().value(0)), Foo);
BOOST_TEST_TRAIT_SAME(decltype(std::declval<axis::category<int>>().value(0)), int);
}
// axis::category with growth