你如何使用带有提升格式的CAtlStringW

How do you use a CAtlStringW with boost format?

本文关键字:格式 CAtlStringW 何使用      更新时间:2023-10-16

我试过这个:

#include <iostream>
#include <boostformat.hpp>
#include <atlstr.h>
std::ostream& operator<<(std::ostream& os, const ATL::CAtlStringW& string)
{
    return os << string.GetString();
}
int _tmain(int argc, _TCHAR* argv[])
{
    CAtlStringW world = L"world";
    boost::wformat formatter(L"hello %s");
    formatter % world;
    std::wstring formatted = formatter.str();
    return 0;
}

并格式化为"hello 004B54D8",但我希望它是"hello world"。我已经尝试了一些变体,例如在命名空间中定义运算符<<。我错过了什么?接线员<<似乎没有被调用。

谢谢。

提升格式文档提供了以下用于格式化自定义类型的示例:http://www.boost.org/doc/libs/1_49_0/libs/format/example/sample_userType.cpp

Doh。

我错过了"w":

std::wostream& operator<<(std::wostream& os, const ATL::CAtlStringW& string)
{
    return os << string.GetString();
}