1

我有一个简单的模板,有点像:

template <typename T, T Min, T Max>
class LimitedInt {
public:
   static_assert(Min < Max, "Min must be less than Max");
   explicit LimitedInt(const T value)
   {
      setValue(value);
   }
   void setValue(const T value)
   {
      if (value < Min || value > Max) {
         throw std::invalid_argument("invalid value");
      }
      mValue = value;
   }
   T getValue() const
   {
      return mValue;
   }
private:
   T mValue{Min};
}

这使我可以将其专门化为:

using Vlan = LimitedInt<uint16_t, 0, 4094>;

我希望能够用类似的东西格式化值

Vlan v{42};
fmt::format("{:04x}", v);

为此,我尝试将格式化职责转发到此处所述,formatter<int>但无济于事。我的尝试看起来像:

namespace fmt {
template <>
struct formatter<LimitedInt> {
   formatter<int> int_formatter;
   template <typename ParseContext>
   constexpr auto parse(ParseContext& ctx)
   {
      return int_formatter.parse(ctx);
   }
   template <typename FormatContext>
   auto format(const LimtedInt& li, FormatContext& ctx)
   {
      return int_formatter.format(li.getValue(), ctx);
   }
};
}  // namespace fmt

我已经尝试了几种变体但没有成功,错误往往围绕着这个:

In file included from /output/build/proj-local/src/networkinterface.h:8:0,
                 from /output/build/proj-local/src/networkinterface.cpp:3:
/output/build/proj-local/src/limitedints.h:78:38: error: type/value mismatch at argument 1 in template parameter list for 'template<class T, class Char, class Enable> struct fmt::v7::formatter'
 struct formatter<LimitedInt> {
                                      ^
/output/build/proj-local/src/limitedints.h:78:38: note:   expected a type, got 'LimitedInt'

我目前的解决方法是拥有一个成熟的格式化程序,它有自己的parse()方法format(),但对我来说,重新发明 Victor 已经写的轮子充其量是愚蠢的。

4

2 回答 2

1

通常的专业化规则适用。具体来说,您应该制作formatter一个模板并将模板参数传递给LimitedInt

template <typename T, T Min, T Max>
struct fmt::formatter<LimitedInt<T, Min, Max>> {
  constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }

  auto format(const LimitedInt<T, Min, Max>& val, format_context& ctx) {
    // Format val and write the output to ctx.out().
    return ctx.out();
  }
};

神螺栓: https ://godbolt.org/z/Mnjsbc

于 2021-01-19T00:49:21.060 回答
0

我最终得到的是:

namespace fmt {
template <typename T, T Min, T Max>
struct formatter<LimitedInt<T, Min, Max>> {
   formatter<int> int_formatter;
   constexpr auto parse(format_parse_context& ctx)
   {
      return int_formatter.parse(ctx);
   }
   auto format(const LimitedInt<T, Min, Max>& li, format_context& ctx)
   {
      return int_formatter.format(li.getValue(), ctx);
   }
};
}  // namespace fmt

它通过了我所有的单元测试。在此过程中,我了解了更多关于 C++ 模板的信息。感谢 Werner、Igor 和 Victor,他们帮助我指明了正确的方向。从 C 到 C++ 的过渡并不总是一帆风顺的。

于 2021-01-19T14:34:22.093 回答