From f8fdfb0f5738156fb0ecb586f298b385aa33c569 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 25 Dec 2022 13:58:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E2=80=98storage=5Ft=E2=80=99=20has=20no?= =?UTF-8?q?t=20been=20declared?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/libimp/result.h | 62 ++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/include/libimp/result.h b/include/libimp/result.h index 1fcd614..dd95d95 100644 --- a/include/libimp/result.h +++ b/include/libimp/result.h @@ -88,54 +88,38 @@ struct default_traits { } /// \brief Custom formatted output. - static std::string format(result const &r) noexcept { - return fmt("error = ", r.error()); - } + static std::string format(result const &r); }; template struct default_traits::value>> : generic_traits { /// \brief Custom initialization. - constexpr static void init_code(storage_t &code, T value, bool ok) noexcept { + constexpr static void init_code(typename generic_traits::storage_t &code, + T value, bool ok) noexcept { code = {value, static_cast(ok ? 0 : - ((value == default_value()) ? error_number_limit : value))}; + ((value == 0) ? error_number_limit : value))}; } using generic_traits::init_code; - /// \brief Custom default value. - constexpr static T default_value() noexcept { - return 0; - } - /// \brief Custom formatted output. - static std::string format(result const &r) noexcept { - return fmt(*r); - } + static std::string format(result const &r) noexcept; }; template struct default_traits::value>> : generic_traits { /// \brief Custom initialization. - constexpr static void init_code(storage_t &code, std::nullptr_t, error_code const &ec) noexcept { + constexpr static void init_code(typename generic_traits::storage_t &code, + std::nullptr_t, error_code const &ec) noexcept { code = {nullptr, ec}; } - constexpr static void init_code(storage_t &code, std::nullptr_t) noexcept { + constexpr static void init_code(typename generic_traits::storage_t &code, + std::nullptr_t) noexcept { code = {nullptr, -1}; } using generic_traits::init_code; - /// \brief Custom default value. - constexpr static T default_value() noexcept { - return nullptr; - } - /// \brief Custom formatted output. - static std::string format(result const &r) noexcept { - if LIBIMP_LIKELY(r) { - return fmt(static_cast(*r)); - } - return fmt(static_cast(*r), ", error = ", r.error()); - } + static std::string format(result const &r) noexcept; }; } // namespace detail_result @@ -194,14 +178,34 @@ public: bool ok () const noexcept { return type_traits_t::get_ok (code_); } error_code error() const noexcept { return type_traits_t::get_error(code_); } - explicit operator bool() const noexcept { return ok (); } + explicit operator bool() const noexcept { return ok(); } friend bool operator==(result const &lhs, result const &rhs) noexcept { return lhs.code_ == rhs.code_; } friend bool operator!=(result const &lhs, result const &rhs) noexcept { return !(lhs == rhs); } }; /// \brief Custom defined fmt_to method for imp::fmt -namespace detail { +namespace detail_result { + +template +std::string default_traits::format(result const &r) { + return fmt("error = ", r.error()); +} + +template +std::string default_traits::value>> + ::format(result const &r) noexcept { + return fmt(*r); +} + +template +std::string default_traits::value>> + ::format(result const &r) noexcept { + if LIBIMP_LIKELY(r) { + return fmt(static_cast(*r)); + } + return fmt(static_cast(*r), ", error = ", r.error()); +} template bool tag_invoke(decltype(::LIBIMP::fmt_to), fmt_context &ctx, result r) { @@ -213,5 +217,5 @@ bool tag_invoke(decltype(::LIBIMP::fmt_to), fmt_context &ctx, result r) return fmt_to(ctx, (r ? "succ" : "fail"), ", ", result::type_traits_t::format(r)); } -} // namespace detail +} // namespace detail_result LIBIMP_NAMESPACE_END_