我正在使用POCO(v 1.4.6p4)日志记录框架,但是在格式化字符串时遇到一些问题:
int MyClass::MyMethod(unsigned short number) {
Logger::get("MyLog").information(Poco::format("Log a number [%u]", number));
}
在这里我得到:
Log a number [ERRFMT]
然后抛出Poco::BadCastException。
深入研究源代码,我注意到该异常被抛出到Any类中:
template <typename ValueType>
ValueType AnyCast(const Any& operand)
/// AnyCast operator used to extract a copy of the ValueType from an const Any&.
///
/// Example Usage:
/// MyType tmp = AnyCast<MyType>(anAny).
/// Will throw a BadCastException if the cast fails.
/// Dont use an AnyCast in combination with references, i.e. MyType& tmp = ... or const MyType& = ...
/// Some compilers will accept this code although a copy is returned. Use the RefAnyCast in
/// these cases.
{
ValueType* result = AnyCast<ValueType>(const_cast<Any*>(&operand));
if (!result) throw BadCastException("Failed to convert between const Any types");
return *result;
}
有人可以告诉我我错了吗?
最佳答案
您必须添加modifier(h)来实现无符号缩写:
Poco::format("Log a number [%hu]", number)
这种烦恼来自任何人从它那里提取值的严格性-您必须要求确切的类型,否则,您只能获得异常或空指针。