| 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 | |
| 10 | template<class T> |
| 11 | struct auto_caster |
| 12 | { |
| 13 | template<class U> |
| 14 | T operator()(U x) |
| 15 | { |
| 16 | return T(x); |
| 17 | } |
| 18 | }; |
| 19 | |
| 20 | template<class T> |
| 21 | struct auto_caster_noexcept |
| 22 | { |
| 23 | template<class U> |
| 24 | T operator()(U x) noexcept |
| 25 | { |
| 26 | return T(x); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | struct auto_caster_foo |
| 31 | { |
| 32 | int i; |
| 33 | explicit auto_caster_foo(int ip) : i(ip) {} |
| 34 | |
| 35 | }; |
| 36 | // TODO: Test template constraint on conversion operator |
| 37 | static constexpr boost::hof::implicit<auto_caster> auto_cast = {}; |
| 38 | |
| 39 | BOOST_HOF_TEST_CASE() |
| 40 | { |
| 41 | float f = 1.5; |
| 42 | int i = auto_cast(f); |
| 43 | // auto_caster_foo x = 1; |
| 44 | auto_caster_foo x = auto_cast(1); |
| 45 | BOOST_HOF_TEST_CHECK(1 == i); |
| 46 | BOOST_HOF_TEST_CHECK(1 == x.i); |
| 47 | |
| 48 | } |
| 49 | #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION |
| 50 | BOOST_HOF_TEST_CASE() |
| 51 | { |
| 52 | boost::hof::implicit<auto_caster_noexcept> lauto_cast{}; |
| 53 | float f = 1.5; |
| 54 | static_assert(noexcept(int(lauto_cast(f))), "noexcept implicit" ); |
| 55 | } |
| 56 | #endif |
| 57 | |