3

我正在尝试编写一个自定义格式化程序来帮助我打印矢量。我试图维护格式说明符,以便向量中的每个项目都以相同的方式格式化。

我从本教程文档中获得了大部分灵感

#include <fmt/core.h>
#include <fmt/format.h>

template<typename ValueType>
struct fmt::formatter<std::vector<ValueType>> : fmt::formatter<ValueType>
{
    std::string formatString;

    // Hack, copy the original format text into a std::string
    constexpr auto parse(format_parse_context& ctx)
    {
        formatString= "{:";
        for (auto iter = std::begin(ctx); iter  != std::end(ctx); ++iter) {
            char c = *iter;{
                formatString += c;
            }
            if (c == '}') {
                return iter;
            }
        }
        return std::end(ctx);
    }

    template <typename FormatContext>
    auto format(const std::vector<ValueType>& container, FormatContext& context)
    {
        auto&& out = context.out();
        format_to(out, "{{");

        typename std::vector<ValueType>::size_type count = 0;
        const typename std::vector<ValueType>::size_type size = container.size();
        for (const auto& item : container) {
            // Use the copied format string, but really want to delegate the formatting to superclass...
            format_to(out, formatString, item);
            if (++count < size) {
                format_to(out, ", ");
            }
        }

        return format_to(out, "}}");
    }
};

int main()
{
    fmt::print("{:.3f}\n", std::vector{ 0.0, 321.123, 654398.4328, -0.0000000000012345, 2374651273.7236457862345});
    fmt::print("{:.1e}\n", std::vector{ 0.0, 321.123, 654398.4328, -0.0000000000012345, 2374651273.7236457862345});
    return 0;
}

哪个输出:

{0.000, 321.123, 654398.433, -0.000, 2374651273.724}
{0.0e+00, 3.2e+02, 6.5e+05, -1.2e-12, 2.4e+09}

复制格式字符串似乎过于笨重且效率低下,以便我可以将其反馈给另一个fmt::format调用,尤其是当扩展类: fmt::formatter<ValueType>已经在parse内部为我们提供了一个完全有效的函数时(我在这个例子中重新实现了它只是为了以一种骇人听闻的方式获得所需的输出)。

我真的很想删除自定义解析实现并替换该行

format_to(out, formatString, item);

format_to(out, fmt::formatter<ValueType>::format(item, context))

除非它无效/无法编译。

这样做的正确方法是什么?


注意:我完全意识到在我的示例中扩展类型是没有意义的,并且我可以将它作为局部变量但是我正在尝试重用类的功能,所以扩展它感觉是正确的方向,即使我还没有找到解决方案。


我发现的所有其他示例的列表还没有帮助我:

4

1 回答 1

3

您可以摆脱您的parse实现并使用继承的函数,并fmt::formatter<ValueType>::format(item, context)在您的内部使用format来输出每个项目(godbolt demo):

#include <fmt/core.h>
#include <fmt/format.h>

template<typename ValueType>
struct fmt::formatter<std::vector<ValueType>> : fmt::formatter<ValueType>
{
    template <typename FormatContext>
    auto format(const std::vector<ValueType>& container, FormatContext& context)
    {
        auto&& out = context.out();
        format_to(out, "{{");

        bool first = true;
        for (const auto& item : container) {
            if (first) {
                first = false;
            } else {
                format_to(out, ", ");
            }
            fmt::formatter<ValueType>::format(item, context);
        }

        return format_to(out, "}}");
    }
};

int main()
{
    fmt::print("{:.3f}\n", std::vector{ 0.0, 321.123, 654398.4328, -0.0000000000012345, 2374651273.7236457862345});
    fmt::print("{:.1e}\n", std::vector{ 0.0, 321.123, 654398.4328, -0.0000000000012345, 2374651273.7236457862345});
    return 0;
}

您可以在Formatting User-defined Types下的文档中查看此模式的其他示例。

于 2021-03-22T19:14:57.783 回答