// Copyright (c) 2019 Robert Ramey // // 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) // compile only test to test constexpr casting #include #include #include template using safe_t = boost::safe_numerics::safe< T, boost::safe_numerics::native, boost::safe_numerics::trap_exception >; constexpr const char * test_casting_results[] = { // 0 0 0 0 // 012345670123456701234567012345670 // 012345678901234567890123456789012 /* 0*/ ".....xxx.xxx.xxx.xxx.xxx.xxx.xxx.", /* 1*/ ".........xxx.xxx...x.xxx.xxx.xxx.", /* 2*/ ".............xxx...x...x.xxx.xxx.", /* 3*/ "...................x...x...x.xxx.", /* 4*/ "..xx.xxx.xxx.xxx..xx.xxx.xxx.xxx.", /* 5*/ "..xx..xx.xxx.xxx......xx.xxx.xxx.", /* 6*/ "..xx..xx..xx.xxx..........xx.xxx.", /* 7*/ "..xx..xx..xx..xx..............xx.", }; #include using namespace boost::safe_numerics; #include using namespace boost::mp11; template struct p { constexpr static bool value = '.' == test_casting_results[mp_first::value][mp_second::value]; }; template constexpr bool test_cast_constexpr(const T1 & v1){ // if we don't expect the operation to pass, we can't // check the constexpr version of the calculation so // just return success. #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-value" static_cast>(v1); static_cast(v1); #pragma GCC diagnostic pop return true; } #include "test_values.hpp" template struct test { static_assert(mp_is_list(), "must be a list of two indices"); const static std::size_t i = mp_first(); const static std::size_t j = mp_second(); using T = mp_at_c; // first element is a type using T1 = typename mp_at_c::value_type; const static T1 v = mp_at_c::value; const static bool value = test_cast_constexpr(make_safe_literal(v, native, trap_exception)); }; int main(){ using namespace boost::safe_numerics; using type_indices = mp_iota_c::value>; using value_indices = mp_iota_c::value>; // generate all combinations of types <- value using l = mp_product; //boost::safe_numerics::utility::print_types lp; // filter out the invalid ones using l1 = mp_copy_if; //boost::safe_numerics::utility::print_types l1p; // verify that all valid ones compile without error static_assert(mp_all_of(), "testing all valid casts"); return 0; }