From fa8ef7d138913d2f10482b0f1693ad94ce497f11 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Thu, 15 Jun 2023 09:38:46 +0200 Subject: refactor: Avoid copy of bilingual_str when formatting, Fix ADL violation The return type of TranslateArg is std::string, which creates a copy. Fix this by moving everything into a lambda that takes a reference and returns a reference. Also, the format function is called without specifying the namespace it lives in. Fix this by specifying the namespace. See also: https://github.com/bitcoin/bitcoin/blob/7a59865793cd710d7d6650a6106ca4e790ced5d3/doc/developer-notes.md#L117-L137. --- src/util/translation.h | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/util/translation.h b/src/util/translation.h index d2b49d00b0..d33fd2d0a0 100644 --- a/src/util/translation.h +++ b/src/util/translation.h @@ -49,22 +49,18 @@ inline bilingual_str Untranslated(std::string original) { return {original, orig // Provide an overload of tinyformat::format which can take bilingual_str arguments. namespace tinyformat { -inline std::string TranslateArg(const bilingual_str& arg, bool translated) -{ - return translated ? arg.translated : arg.original; -} - -template -inline T const& TranslateArg(const T& arg, bool translated) -{ - return arg; -} - template bilingual_str format(const bilingual_str& fmt, const Args&... args) { - return bilingual_str{format(fmt.original, TranslateArg(args, false)...), - format(fmt.translated, TranslateArg(args, true)...)}; + const auto translate_arg{[](const auto& arg, bool translated) -> const auto& { + if constexpr (std::is_same_v) { + return translated ? arg.translated : arg.original; + } else { + return arg; + } + }}; + return bilingual_str{tfm::format(fmt.original, translate_arg(args, false)...), + tfm::format(fmt.translated, translate_arg(args, true)...)}; } } // namespace tinyformat -- cgit v1.2.3