From d96f749a5200ac60a1d6819c6accceadf83edcfb Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Wed, 19 Dec 2018 08:50:18 +0100 Subject: [PATCH] deduction guides for histogram --- CMakeLists.txt | 4 +- include/boost/histogram/histogram.hpp | 11 ++++ test/axis_deduction_guide_test.cpp | 16 ++++- test/deduction_guides_test.cpp | 87 +++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 test/deduction_guides_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index aae1144e..06cd5652 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,8 +145,8 @@ if (TEST_RANGE_SUPPORT) # test support for external boost::range endif() if (cxx_std_17 IN_LIST CMAKE_CXX_COMPILE_FEATURES) # test support for optional C++17 features - compiled_test(test/axis_deduction_guide_test.cpp) - target_compile_features(axis_deduction_guide_test PRIVATE cxx_std_17) + compiled_test(test/deduction_guides_test.cpp) + target_compile_features(deduction_guides_test PRIVATE cxx_std_17) endif() if (BUILD_BENCHMARKS) diff --git a/include/boost/histogram/histogram.hpp b/include/boost/histogram/histogram.hpp index 8f94d67d..efc540a5 100644 --- a/include/boost/histogram/histogram.hpp +++ b/include/boost/histogram/histogram.hpp @@ -191,6 +191,17 @@ private: friend struct unsafe_access; }; +#if __cpp_deduction_guides >= 201606 + +template +histogram(Axes&& axes)->histogram, default_storage>; + +template +histogram(Axes&& axes, Storage&& storage) + ->histogram, detail::unqual>; + +#endif + } // namespace histogram } // namespace boost diff --git a/test/axis_deduction_guide_test.cpp b/test/axis_deduction_guide_test.cpp index aaf2cfff..1fc75169 100644 --- a/test/axis_deduction_guide_test.cpp +++ b/test/axis_deduction_guide_test.cpp @@ -6,8 +6,9 @@ #include #include -#include +#include #include +#include #include using namespace boost::histogram; @@ -82,6 +83,19 @@ int main() { BOOST_TEST_TRAIT_TRUE( (std::is_same>)); } + + { + auto a = histogram(std::make_tuple(axis::regular(3, -1, 1), axis::integer(0, 4))); + BOOST_TEST_EQ(a.rank(), 2); + BOOST_TEST_EQ(a.axis(0), axis::regular(3, -1, 1)); + BOOST_TEST_EQ(a.axis(1), axis::integer(0, 4)); + + std::vector> axes{{axis::regular(3, -1, 1), axis::regular(5, 0, 5)}}; + auto b = histogram(axes, weighted_storage()); + BOOST_TEST_EQ(b.rank(), 2); + BOOST_TEST_EQ(b.axis(0), axis::regular(3, -1, 1)); + BOOST_TEST_EQ(b.axis(1), axis::regular(5, 0, 5)); + } #endif return boost::report_errors(); } diff --git a/test/deduction_guides_test.cpp b/test/deduction_guides_test.cpp new file mode 100644 index 00000000..aaf2cfff --- /dev/null +++ b/test/deduction_guides_test.cpp @@ -0,0 +1,87 @@ +// Copyright 2018 Hans Dembinski +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt +// or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include + +using namespace boost::histogram; +namespace tr = axis::transform; + +// tests requires a C++17 compatible compiler + +int main() { +#if __cpp_deduction_guides >= 201611 + { + axis::regular a(1, 0.0, 1.0); + axis::regular b(1, 0, 1); + axis::regular c(1, 0.0f, 1.0f); + axis::regular d(1, 0, 1, "foo"); + axis::regular e(1, 0, 1, axis::null_type{}); + axis::regular f(tr::sqrt(), 1, 0, 1); + axis::regular g(tr::sqrt(), 1, 0, 1, "foo"); + axis::regular h(tr::sqrt(), 1, 0, 1, axis::null_type{}); + + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE( + (std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE( + (std::is_same>)); + } + + { + axis::integer a(1, 2); + axis::integer b(1l, 2l); + axis::integer c(1.0, 2.0); + axis::integer d(1.0f, 2.0f); + axis::integer e(1, 2, "foo"); + axis::integer f(1, 2, axis::null_type{}); + + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE( + (std::is_same>)); + } + + { + axis::variable a{-1, 1}; + axis::variable b{-1.f, 1.f}; + axis::variable c{-1.0, 1.0}; + axis::variable d({-1, 1}, "foo"); + axis::variable e({-1, 1}, axis::null_type{}); + + std::vector vi{{-1, 1}}; + std::vector vf{{-1.f, 1.f}}; + axis::variable f(vi); + axis::variable g(vf); + axis::variable h(vi, "foo"); + axis::variable i(vi, axis::null_type{}); + + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE( + (std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE((std::is_same>)); + BOOST_TEST_TRAIT_TRUE( + (std::is_same>)); + } +#endif + return boost::report_errors(); +}