【问题标题】:Poco::format errorPoco::格式错误
【发布时间】:2014-06-03 09:02:49
【问题描述】:

我正在使用 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;
}

谁能告诉我哪里错了?

【问题讨论】:

    标签: c++ poco-libraries


    【解决方案1】:

    你必须为 unsigned short 添加modifier (h):

    Poco::format("Log a number [%hu]", number)
    

    这种烦恼来自于 Any 在从中提取值方面的严格性 - 您必须询问确切的类型,否则您只能得到一个异常或空指针。

    【讨论】:

    • 另一个选项是%?u,它将接受任何无符号整数类型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多