无法使用 Boost Format() 从 wstring 转换

Posted

技术标签:

【中文标题】无法使用 Boost Format() 从 wstring 转换【英文标题】:Cannot convert from wstring with Boost Format() 【发布时间】:2016-06-10 01:59:54 【问题描述】:

我有一个异常类定义如下:

class DeviceOpenException : public std::runtime_error 
public:
    DeviceOpenException(const std::string& message)
        : std::runtime_error("Device Open Exception: " + message)  ;
; 

我抛出异常如下:

throw DeviceOpenException(boost::str(boost::format("Cannot Open HID Device [Vendor ID = %1%] [Product ID = %2%] [Serial Number = %3%]") % vendorID %productID % serialNumber));

其中给former的参数如下:

(unsigned short vendorID, unsigned short productID, std::wstring serialNumber)

但是,这会导致以下编译错误:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::wstring' (or there is no acceptable conversion)

是我,还是 Boost 格式化程序不适用于 wstring 类型?

【问题讨论】:

【参考方案1】:

对宽字符串使用boost::wformat。请参阅文档here。

注意 typedef:

typedef basic_format&lt;wchar_t &gt; wformat;

【讨论】:

以上是关于无法使用 Boost Format() 从 wstring 转换的主要内容,如果未能解决你的问题,请参考以下文章