Я пытался передать строку формата, которая будет использоваться при вызове STD :: format и не могу найти способ. Возможно ли это, или мне нужно создать метод для каждого ожидаемого формата?#include
#include
using namespace std;
class mytest
{
int m_one = 1;
int m_two = 2;
public:
string toString(const string& fmt) const
{
// this works
// return format("{}-{}", m_one, m_two);
// this fails
return format(fmt, m_one, m_two);
}
};
const string MY_FMT = "get {} and add {}";
int main(int argc, char **argv)
{
mytest mt;
string res = mt.toString(MY_FMT);
cout
sample.cpp:17:23: error: call to consteval function 'std::basic_format_string::basic_format_string' is not a constant expression
17 | return format(fmt, m_one, m_two);
| ^
sample.cpp:17:23: note: function parameter 'fmt' with unknown value cannot be used in a constant expression
Подробнее здесь: https://stackoverflow.com/questions/795 ... -stdformat