implicit.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*=============================================================================
  2. Copyright (c) 2017 Paul Fultz II
  3. implicit.cpp
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/hof/implicit.hpp>
  8. #include "test.hpp"
  9. template<class T>
  10. struct auto_caster
  11. {
  12. template<class U>
  13. T operator()(U x)
  14. {
  15. return T(x);
  16. }
  17. };
  18. template<class T>
  19. struct auto_caster_noexcept
  20. {
  21. template<class U>
  22. T operator()(U x) noexcept
  23. {
  24. return T(x);
  25. }
  26. };
  27. struct auto_caster_foo
  28. {
  29. int i;
  30. explicit auto_caster_foo(int ip) : i(ip) {}
  31. };
  32. // TODO: Test template constraint on conversion operator
  33. static constexpr boost::hof::implicit<auto_caster> auto_cast = {};
  34. BOOST_HOF_TEST_CASE()
  35. {
  36. float f = 1.5;
  37. int i = auto_cast(f);
  38. // auto_caster_foo x = 1;
  39. auto_caster_foo x = auto_cast(1);
  40. BOOST_HOF_TEST_CHECK(1 == i);
  41. BOOST_HOF_TEST_CHECK(1 == x.i);
  42. }
  43. #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION
  44. BOOST_HOF_TEST_CASE()
  45. {
  46. boost::hof::implicit<auto_caster_noexcept> lauto_cast{};
  47. float f = 1.5;
  48. static_assert(noexcept(int(lauto_cast(f))), "noexcept implicit");
  49. }
  50. #endif