diff --git a/include/eve/module/core/constant/as_value.hpp b/include/eve/module/core/constant/as_value.hpp index 82cca5b564..2de8ae9a33 100644 --- a/include/eve/module/core/constant/as_value.hpp +++ b/include/eve/module/core/constant/as_value.hpp @@ -10,59 +10,74 @@ #include #include #include +#include #include #include namespace eve { -//================================================================================================ -//! @addtogroup core_conversions -//! @{ -//! @var as_value -//! @brief converts eve constant or just a value to a type. -//! -//! **Defined in Header** -//! -//! @code -//! #include -//! @endcode -//! -//! @groupheader{Callable Signatures} -//! -//! @code -//! namespace eve -//! { -//! template< eve::value From, eve::value T > -//! T as_value(From x, eve::as t) noexcept; -//! } -//! @endcode -//! -//! **Parameters** -//! -//! * `x`: either something convertible to T or an of eve constant -//! * `t` : [Type wrapper](@ref eve::as) instance embedding the type of the constant. -//! -//! **Return value** -//! -//! The call `eve::as_value(as())` returns a value of type T which is -//! the conversion of `x` to type Target or the the eve constant of type Target. -//! -//! @groupheader{Example} -//! @godbolt{doc/core/constant/as_value.cpp} -//================================================================================================ - EVE_MAKE_CALLABLE(as_value_, as_value); - - namespace detail + template + struct as_value_t : callable { - template - EVE_FORCEINLINE constexpr auto as_value_(EVE_SUPPORTS(cpu_), From from, as const& t) noexcept + template + EVE_FORCEINLINE constexpr T operator()(From from, as t) const noexcept + { + return from(t); + } + + template + EVE_FORCEINLINE constexpr T operator()(From from, as) const noexcept + { + return T{from}; + } + + template + EVE_FORCEINLINE constexpr T operator()(From from, as) const noexcept { - if constexpr( requires { typename From::constant_callable_tag; } ) return from(t);//if constexpr( instance_of ) return from(t); - else if constexpr( scalar_value ) return static_cast(from); - else return T {from}; + return T{from}; } - } + }; + + //================================================================================================ + //! @addtogroup core_conversions + //! @{ + //! @var as_value + //! @brief converts eve constant or just a value to a type. + //! + //! **Defined in Header** + //! + //! @code + //! #include + //! @endcode + //! + //! @groupheader{Callable Signatures} + //! + //! @code + //! namespace eve + //! { + //! template + //! T as_value(From x, eve::as t) noexcept; + //! + //! template + //! T as_value(From from, eve::as t) noexcept; + //! } + //! @endcode + //! + //! **Parameters** + //! + //! * `x`: either something convertible to T or an of eve constant + //! * `t` : [Type wrapper](@ref eve::as) instance embedding the type of the constant. + //! + //! **Return value** + //! + //! The call `eve::as_value(as())` returns a value of type T which is + //! the conversion of `x` to type Target or the the eve constant of type Target. + //! + //! @groupheader{Example} + //! @godbolt{doc/core/constant/as_value.cpp} + //================================================================================================ + inline constexpr auto as_value = functor; //================================================================================================ //! @} //================================================================================================ diff --git a/test/doc/core/constant/as_value.cpp b/test/doc/core/constant/as_value.cpp index ca25eae6ed..8e828a6806 100644 --- a/test/doc/core/constant/as_value.cpp +++ b/test/doc/core/constant/as_value.cpp @@ -12,11 +12,8 @@ int main() << "<- as_value(valmax, as()) = " << eve::as_value(eve::valmax, eve::as()) << '\n' ; - double xf = -62768.0f; - std::cout << "---- scalar" << '\n' - << "<- as_value(xf, as()) = " << eve::as_value(xf, eve::as()) << '\n' - << "<- as_value(xf, as()) = " << eve::as_value(xf, eve::as()) << '\n'; + << "<- as_value(valmax, as()) = " << eve::as_value(eve::valmax, eve::as()) << '\n'; return 0; } diff --git a/test/unit/module/core/as_value.cpp b/test/unit/module/core/as_value.cpp index a4acee579b..c2ed82d7cf 100644 --- a/test/unit/module/core/as_value.cpp +++ b/test/unit/module/core/as_value.cpp @@ -48,33 +48,32 @@ TTS_CASE_TPL("Check behavior of arithmetic as_value", eve::test::simd::all_types e_t expected {0}; e_t actual = eve::as_value(eve::zero, eve::as {}); TTS_EQUAL(expected, actual); - actual = eve::as_value(0, eve::as {}); - TTS_EQUAL(expected, actual); actual = eve::as_value(expected, eve::as {}); TTS_EQUAL(expected, actual); } }; TTS_CASE_TPL("Check behavior of logical as_value", eve::test::simd::all_types) -(tts::type) {{using U = eve::logical; -U expected {true}; -U actual = eve::as_value(eve::true_, eve::as {}); -TTS_EQUAL(expected, actual); -actual = eve::as_value(true, eve::as {}); -TTS_EQUAL(expected, actual); -actual = eve::as_value(expected, eve::as {}); -TTS_EQUAL(expected, actual); -} +(tts::type) { + { + using U = eve::logical; + U expected {true}; + U actual = eve::as_value(eve::true_, eve::as {}); + TTS_EQUAL(expected, actual); + actual = eve::as_value(true, eve::as {}); + TTS_EQUAL(expected, actual); + actual = eve::as_value(expected, eve::as {}); + TTS_EQUAL(expected, actual); + } -{ - using U = eve::logical>; - U expected {true}; - U actual = eve::as_value(eve::true_, eve::as {}); - TTS_EQUAL(expected, actual); - actual = eve::as_value(true, eve::as {}); - TTS_EQUAL(expected, actual); - actual = eve::as_value(expected, eve::as {}); - TTS_EQUAL(expected, actual); -} -} -; + { + using U = eve::logical>; + U expected {true}; + U actual = eve::as_value(eve::true_, eve::as {}); + TTS_EQUAL(expected, actual); + actual = eve::as_value(true, eve::as {}); + TTS_EQUAL(expected, actual); + actual = eve::as_value(expected, eve::as {}); + TTS_EQUAL(expected, actual); + } +};