diff --git a/include/duckhandy/int_conv.hpp b/include/duckhandy/int_conv.hpp index a092046..b455c9d 100644 --- a/include/duckhandy/int_conv.hpp +++ b/include/duckhandy/int_conv.hpp @@ -22,27 +22,37 @@ #include #include #include +#include namespace dhandy { namespace implem { - template + template struct IntConv; - template - struct IntConv, std::string>, F> { + template + struct IntConv, std::string>, F, SafeRetval> { static std::string conv (const F& in) { auto retval = dhandy::int_to_ary(in); return std::string(retval.begin(), retval.end() - 1); } }; - template - struct IntConv, std::string>> { - static T conv (const std::string& in) { + template + struct IntConv, std::string_view>, F, SafeRetval> { + constexpr static std::string_view conv (const F& in) { + if (std::is_constant_evaluated() or not SafeRetval) + return dhandy::int_to_ary(in).to_string_view(); + else + throw std::logic_error("Only callable in a constexpr context, call int_conv_temporary() instead if you know what you're doing"); + } + }; + template + struct IntConv, std::string>, SafeRetval> { + constexpr static T conv (const std::string& in) { return dhandy::ary_to_int(in.data(), in.data() + in.size()); } }; - template - struct IntConv, std::string_view>> { + template + struct IntConv, std::string_view>, SafeRetval> { constexpr static T conv (const std::string_view& in) { return dhandy::ary_to_int(in.data(), in.data() + in.size()); } @@ -51,7 +61,12 @@ namespace dhandy { template constexpr inline To int_conv (const From& from) { - return implem::IntConv::conv(from); + return implem::IntConv::conv(from); + } + + template + constexpr inline To int_conv_temporary (const From& from) { + return implem::IntConv::conv(from); } } //namespace dhandy