【发布时间】:2021-09-10 09:00:14
【问题描述】:
我正在尝试将 {fmt} 添加到我的项目中,一切进展顺利,但在尝试为我的简单 Vec2 类添加用户定义类型时遇到了一点问题。
struct Vec2 { float x; float y; };
我希望能够使用与基本内置浮点类型相同的格式标志/参数,但将其复制到 vec2 的 x 和 y 成员,并用括号括起来。
例如,只有一个浮点数:
fmt::format("Hello '{0:<8.4f}' World!", 1.234567);
// results in "Hello '1.2346 ' World!"
使用我的 vec2 类:
Vec2 v {1.2345678, 2.3456789};
fmt::format("Hello '{0:<8}' World!", v);
// results in "Hello (1.2346 , 2.3457 )' World!"
但是当我们尝试使用嵌套替换字段时,我复制替换字段内容的简单方法不起作用。 例如带有浮点数:
fmt::format("Hello '{0:<{1}.4f}' World!", 1.234567, 8);
// results in "Hello '1.2346 ' World!"
但是尝试使用我的 Vec2 类型...
Vec2 v {1.2345678, 2.3456789};
fmt::format("Hello '{0:<{1}.4f}' World!", v, 8);
// throws format_error, what(): "argument not found"
当然,这是因为我所做的只是在 ':' 之后和 '}' 之前复制替换字段,并尊重 {} 平衡,所以如果我使用了嵌套替换字段,那么将是一个 {} ,它引用原始列表中的某些参数,这对此没有好处。
我对用户定义类型的专长:
struct Vec2
{
float x;
float y;
};
template<>
struct fmt::formatter<Vec2>
{
auto parse(fmt::format_parse_context& ctx) -> decltype(ctx.begin())
{
int curlyBalance = 1;
auto it = ctx.begin(), end = ctx.end();
while (it != end)
{
if (*it == '}')
{
--curlyBalance;
}
else if (*it == '{')
{
++curlyBalance;
}
if (curlyBalance <= 0)
break;
else
++it;
}
const char* beginPtr = &(*ctx.begin());
const char* endPtr = &(*it);
size_t len = endPtr - beginPtr;
if (len == 0)
{
formatStr = "{}";
}
else
{
formatStr = "{0:";
formatStr += std::string(beginPtr, len + 1);
}
return it;
}
template <typename FormatContext>
auto format(const Vec2& vec, FormatContext& context)
{
fmt::format_to(context.out(), "(");
fmt::format_to(context.out(), formatStr, vec.x);
fmt::format_to(context.out(), ", ");
fmt::format_to(context.out(), formatStr, vec.y);
return fmt::format_to(context.out(), ")");
}
std::string formatStr;
};
int main()
{
std::cout << "Hello world!" << std::endl;
Vec2 v {1.234567, 2.345678};
// Simple, static width.
//std::string fmtResult = fmt::format("Hello '{0:<8.4f}' World!\n", v, 5);
// Dynamic width, oh god, oh dear god no!
std::string fmtResult = fmt::format("Hello '{0:<{1}}' World!\n", v, 5);
std::cout << fmtResult;
}
似乎需要发生的是我的解析函数需要访问其他参数,以便它可以用正确的值填充嵌套替换字段......但我仍然是这个库的新手,并且会非常感谢一些帮助!
【问题讨论】:
标签: c++ nested user-defined-types specialization fmt