diff options
author | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-06-15 09:38:46 +0200 |
---|---|---|
committer | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-06-15 16:21:29 +0200 |
commit | fa8ef7d138913d2f10482b0f1693ad94ce497f11 (patch) | |
tree | 2f72cde98d136352f29bfda9253cf972009b177e /src/util | |
parent | 681ecac5c2d462920cd32636eec15599a9bcf424 (diff) |
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.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/translation.h | 22 |
1 files 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 <typename T> -inline T const& TranslateArg(const T& arg, bool translated) -{ - return arg; -} - template <typename... Args> 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<decltype(arg), const bilingual_str&>) { + 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 |