detail_static_if_test.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. // Copyright 2015-2017 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/core/lightweight_test.hpp>
  7. #include <boost/histogram/detail/static_if.hpp>
  8. #include "throw_exception.hpp"
  9. using namespace boost::histogram::detail;
  10. int main() {
  11. using T = std::true_type;
  12. using F = std::false_type;
  13. // check that branch not taken does not have to compile
  14. BOOST_TEST_EQ(static_if<T>([](auto) { return 1; }, [] {}, 0), 1);
  15. BOOST_TEST_EQ(static_if<F>([] {}, [](auto) { return 1; }, 0), 1);
  16. BOOST_TEST_EQ(static_if_c<true>([](auto) { return 1; }, [] {}, 0), 1);
  17. BOOST_TEST_EQ(static_if_c<false>([] {}, [](auto) { return 1; }, 0), 1);
  18. // check that noexcept is correctly propagated
  19. auto may_throw = [](auto x) { return x; };
  20. auto no_throw = [](auto x) noexcept { return x; };
  21. // make this work with -fno-exceptions
  22. BOOST_TEST_EQ(noexcept(static_if<F>(no_throw, may_throw, 0)), noexcept(may_throw(0)));
  23. BOOST_TEST(noexcept(static_if<T>(no_throw, may_throw, 0)));
  24. return boost::report_errors();
  25. }