Skip to content

Commit

Permalink
better solution for template restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
HDembinski committed Apr 25, 2024
1 parent 9a7aa5e commit 8125277
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
17 changes: 4 additions & 13 deletions include/boost/histogram/accumulators/collector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,12 @@ class collector {
using size_type = typename container_type::size_type;
using const_pointer = typename container_type::const_pointer;

template <typename... Args>
// make template only match if forwarding args to container is valid
template <typename... Args, class = decltype(container_type(std::declval<Args>()...))>
explicit collector(Args&&... args) : container_(std::forward<Args>(args)...) {}

// we must explicitly implement the copy/move ctors so that they are a
// better match than the generic forwarding template above
collector(const collector& other) : container_(other.container_) {}
collector(collector& other) : container_(other.container_) {}
collector(collector&& other) : container_(std::move(other.container_)) {}

// when copy/move ctors are explicitly implemented, one also has to implement the
// assignment operators
collector& operator=(const collector&) = default;
collector& operator=(collector&&) = default;

template <class T, typename... Args>
// make template only match if forwarding args to container is valid
template <class T, typename... Args, class = decltype(container_type(std::initializer_list<T>(),std::declval<Args>()...))>
explicit collector(std::initializer_list<T> list, Args&&... args)
: container_(list, std::forward<Args>(args)...) {}

Expand Down
44 changes: 22 additions & 22 deletions test/utility_clopper_pearson_interval_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,46 @@ template <class T>
void test() {
const T atol = 0.001;

clopper_pearson_interval<T> iv(deviation{1});
clopper_pearson_interval<T> iv(deviation{1.f});

{
const auto x = iv(0, 1);
BOOST_TEST_IS_CLOSE(x.first, 0.0, atol);
BOOST_TEST_IS_CLOSE(x.second, 0.841, atol);
const auto x = iv(0.f, 1.f);
BOOST_TEST_IS_CLOSE(x.first, 0.f, atol);
BOOST_TEST_IS_CLOSE(x.second, 0.841f, atol);

fraction<T> f(0, 1);
fraction<T> f(0.f, 1.f);
const auto y = iv(f);
BOOST_TEST_IS_CLOSE(y.first, 0.0, atol);
BOOST_TEST_IS_CLOSE(y.second, 0.841, atol);
BOOST_TEST_IS_CLOSE(y.first, 0.f, atol);
BOOST_TEST_IS_CLOSE(y.second, 0.841f, atol);
}

{
const auto x = iv(1, 0);
BOOST_TEST_IS_CLOSE(x.first, 0.158, atol);
BOOST_TEST_IS_CLOSE(x.second, 1.0, atol);
const auto x = iv(1.f, 0.f);
BOOST_TEST_IS_CLOSE(x.first, 0.158f, atol);
BOOST_TEST_IS_CLOSE(x.second, 1.f, atol);

fraction<T> f(1, 0);
fraction<T> f(1.f, 0.f);
const auto y = iv(f);
BOOST_TEST_IS_CLOSE(y.first, 0.158, atol);
BOOST_TEST_IS_CLOSE(y.second, 1.0, atol);
BOOST_TEST_IS_CLOSE(y.first, 0.158f, atol);
BOOST_TEST_IS_CLOSE(y.second, 1.f, atol);
}

{
const auto x = iv(5, 5);
BOOST_TEST_IS_CLOSE(x.first, 0.304, atol);
BOOST_TEST_IS_CLOSE(x.second, 0.695, atol);
const auto x = iv(5.f, 5.f);
BOOST_TEST_IS_CLOSE(x.first, 0.304f, atol);
BOOST_TEST_IS_CLOSE(x.second, 0.695f, atol);
}

{
const auto x = iv(1, 9);
BOOST_TEST_IS_CLOSE(x.first, 0.017, atol);
BOOST_TEST_IS_CLOSE(x.second, 0.294, atol);
const auto x = iv(1.f, 9.f);
BOOST_TEST_IS_CLOSE(x.first, 0.017f, atol);
BOOST_TEST_IS_CLOSE(x.second, 0.294f, atol);
}

{
const auto x = iv(9, 1);
BOOST_TEST_IS_CLOSE(x.first, 0.705, atol);
BOOST_TEST_IS_CLOSE(x.second, 0.982, atol);
const auto x = iv(9.f, 1.f);
BOOST_TEST_IS_CLOSE(x.first, 0.705f, atol);
BOOST_TEST_IS_CLOSE(x.second, 0.982f, atol);
}
}

Expand Down

0 comments on commit 8125277

Please sign in to comment.