Skip to content

Commit fa8ef7d

Browse files
author
MarcoFalke
committed
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.
1 parent 681ecac commit fa8ef7d

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/util/translation.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,18 @@ inline bilingual_str Untranslated(std::string original) { return {original, orig
4949

5050
// Provide an overload of tinyformat::format which can take bilingual_str arguments.
5151
namespace tinyformat {
52-
inline std::string TranslateArg(const bilingual_str& arg, bool translated)
53-
{
54-
return translated ? arg.translated : arg.original;
55-
}
56-
57-
template <typename T>
58-
inline T const& TranslateArg(const T& arg, bool translated)
59-
{
60-
return arg;
61-
}
62-
6352
template <typename... Args>
6453
bilingual_str format(const bilingual_str& fmt, const Args&... args)
6554
{
66-
return bilingual_str{format(fmt.original, TranslateArg(args, false)...),
67-
format(fmt.translated, TranslateArg(args, true)...)};
55+
const auto translate_arg{[](const auto& arg, bool translated) -> const auto& {
56+
if constexpr (std::is_same_v<decltype(arg), const bilingual_str&>) {
57+
return translated ? arg.translated : arg.original;
58+
} else {
59+
return arg;
60+
}
61+
}};
62+
return bilingual_str{tfm::format(fmt.original, translate_arg(args, false)...),
63+
tfm::format(fmt.translated, translate_arg(args, true)...)};
6864
}
6965
} // namespace tinyformat
7066

0 commit comments

Comments
 (0)