C++ FMT C++;库:允许用户为自定义类型设置格式说明符

C++ FMT C++;库:允许用户为自定义类型设置格式说明符,c++,fmt,C++,Fmt,例如,我有一个自定义类型 struct custom_type { double value; }; 我想为此类型设置自定义FMT格式化程序。我做了以下工作,它是有效的: namespace fmt { template <> struct formatter<custom_type> { template <typename ParseContext> constexpr auto parse(Par

例如,我有一个自定义类型

struct custom_type
{
    double value;
};
我想为此类型设置自定义FMT格式化程序。我做了以下工作,它是有效的:

namespace fmt
{
    template <>
    struct formatter<custom_type> {
        template <typename ParseContext>
        constexpr auto parse(ParseContext &ctx) {
        return ctx.begin();
    };

    template <typename FormatContext>
    auto format(const custom_type &v, FormatContext &ctx) {
        return format_to(ctx.begin(), "{}", v.value);
    }
};
我该怎么做

目前,当我设置自定义格式字符串时,我得到

 what():  unknown format specifier

我终于做到了。我会把它保存在这里,以防别人需要它

    template <>
    struct formatter<custom_type> {
        template <typename ParseContext>
        constexpr auto parse(ParseContext &ctx) {
            auto it = internal::null_terminating_iterator<char>(ctx);
            std::string tmp;
            while(*it && *it != '}') 
            {
                tmp += *it;
                ++it;
            }
            m_format=tmp;
            return internal::pointer_from(it);
        }

        template <typename FormatContext>
        auto format(const custom_type &v, FormatContext &ctx) {
            std::string final_string;
            if(m_format.size()>0)
            {   
                final_string="{:"+m_format+"}";
            }
            else
            {
                final_string="{}";
            }
            return format_to(ctx.begin(), final_string, v.value);
        }
        mutable std::string m_format;
    };
模板
结构格式化程序{
模板
constexpr自动解析(ParseContext&ctx){
auto it=internal::null\u终止\u迭代器(ctx);
std::字符串tmp;
而(*it&&*it!='}'))
{
tmp+=*it;
++它;
}
m_格式=tmp;
从(it)返回内部::指针_;
}
模板
自动格式化(const自定义_type&v、FormatContext和ctx){
std::字符串final_字符串;
if(m_format.size()>0)
{   
final_string=“{:“+m_格式+”}”;
}
其他的
{
最后的_字符串=“{}”;
}
返回格式(ctx.begin(),final_字符串,v.value);
}
可变std::字符串m_格式;
};

最简单的解决方案是从
格式化程序
继承
格式化程序

模板结构fmt::格式化程序:格式化程序{
自动格式化(自定义c型、格式化上下文和ctx){
返回格式化程序::格式(c.value,ctx);
}
};

您实际上不需要使用任何内部API,也不需要在
格式中构建格式字符串。只需重用所需的格式化程序的专门化。
    template <>
    struct formatter<custom_type> {
        template <typename ParseContext>
        constexpr auto parse(ParseContext &ctx) {
            auto it = internal::null_terminating_iterator<char>(ctx);
            std::string tmp;
            while(*it && *it != '}') 
            {
                tmp += *it;
                ++it;
            }
            m_format=tmp;
            return internal::pointer_from(it);
        }

        template <typename FormatContext>
        auto format(const custom_type &v, FormatContext &ctx) {
            std::string final_string;
            if(m_format.size()>0)
            {   
                final_string="{:"+m_format+"}";
            }
            else
            {
                final_string="{}";
            }
            return format_to(ctx.begin(), final_string, v.value);
        }
        mutable std::string m_format;
    };
template <> struct fmt::formatter<custom_type> : formatter<double> {
  auto format(custom_type c, format_context& ctx) {
    return formatter<double>::format(c.value, ctx);
  }
};