Код: Выделить всё
template
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;
Код: Выделить всё
Vlan v{42};
fmt::format("{:04x}", v);
Код: Выделить всё
namespace fmt {
template
struct formatter {
formatter int_formatter;
template
constexpr auto parse(ParseContext& ctx)
{
return int_formatter.parse(ctx);
}
template
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 struct fmt::v7::formatter'
struct formatter {
^
/output/build/proj-local/src/limitedints.h:78:38: note: expected a type, got 'LimitedInt'
Подробнее здесь: https://stackoverflow.com/questions/657 ... -using-fmt