Home:ALL Converter>How to concatenate the format string?

How to concatenate the format string?

Ask Time:2020-06-11T15:46:03         Author:CygnusX1

Json Formatter

I would like to concatenate two strings and use them as the format string for the fmt library. One of the obvious choices is to simply concatenate them as regular strings and then pass them into the library:

template<typename... Args>
inline void catfmt(std::string a, std::string b, const Args &... args) {
    std::string c = a+b;
    fmt::print(c, args...);
}

However, c is going to be discarded. So maybe fmt provides a way for it to be skipped altogether? Is there a way to emplace a string into the message and tell the library that it too should be parsed? Something like:

template<typename... Args>
inline void catfmt(std::string a, std::string b, const Args &... args) {
    fmt::print("{:sf}{:sf}", a, b, args...);
}

Where {:sf} woud mean: emplace a string and use it as format.

Author:CygnusX1,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/62319461/how-to-concatenate-the-format-string
yy