用于将数据类型打印到调试控制台的函数
Function to print data type to debug console
在中搜索任何类型的函数都可以帮助将数据打印到调试控制台,但我在StackOverflow
上发现了这个函数,它几乎可以打印任何类型的数据:
template <typename Arg, typename... Args>
void doPrint(Arg&& arg, Args&&... args)
{
std::stringstream out;
out << std::forward<Arg>(arg);
using expander = int[];
(void)expander {
0, (void(out << std::left << std::setw(20) << std::forward<Args>(args)), 0)...
};
OutputDebugStringA(out.str().c_str());
}
但当我试图将wstring
传递给它时,它不起作用,例如:
std::wstring color = L"blue";
doPrint("color: ", color);
我得到这个错误:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::wstring' (or there is no acceptable conversion)
是否也可以支持wstring
?
我试图将std::stringstream
更改为std::wstringstream
,但现在在OutputDebugStringA()
:上出现错误
void OutputDebugStringA(LPCSTR)': cannot convert argument 1 from 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>' to 'LPCSTR'
默认情况下,您不能将std::wstring
打印到基于char
的std::ostream
,如std::stringstream
,因为std::basic_ostream<char>
(也称为std::ostream
(没有为std::wstring
(或const wchar_t*
(定义过载的operator<<
。
您可以为此定义自己的operator<<
,它使用WideCharToMultiByte()
或等效转换将std::wstring
转换为std::string
/char*
,然后打印,例如:
std::ostream& operator<<(std::ostream &os, const std::wstring &wstr)
{
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), nullptr, 0, nullptr, nullptr);
if (len > 0) {
std::string str;
str.resize(len);
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), str.data()/* or: &str[0] */, len, nullptr, nullptr);
os << str;
}
return os;
}
template <typename Arg, typename... Args>
void doPrint(Arg&& arg, Args&&... args)
{
std::stringstream out;
out << std::forward<Arg>(arg);
using expander = int[];
(void)expander {
0, (void(out << std::left << std::setw(20) << std::forward<Args>(args)), 0)...
};
OutputDebugStringA(out.str().c_str());
}
在线演示
否则,您需要一个基于wchar_t
的std::basic_ostream<wchar_t>
(也称为std::wostream
(,例如std::wstringstream
,使用OutputDebugStringW()
进行日志记录(除非您将输出std::wstring
转换为std::string
以使用OutputDebugStringA()
进行日志记录(。幸运的是,无论CharT
是char
还是wchar_t
,std::basic_ostream<CharT>
都有一个重载的operator<<
用于const char*
字符串(而不是std::string
(,因此std::wstringstream
可以打印std::string::c_str()
返回的指针,例如:
std::wostream& operator<<(std::wostream &os, const std::string &str)
{
os << str.c_str();
return os;
}
template <typename Arg, typename... Args>
void doPrint(Arg&& arg, Args&&... args)
{
std::wostringstream out;
out << std::forward<Arg>(arg);
using expander = int[];
(void)expander {
0, (void(out << std::left << std::setw(20) << std::forward<Args>(args)), 0)...
};
OutputDebugStringW(out.str().c_str());
}
在线演示
- C++(控制台)构造函数和派生类
- 如何在关闭Qt控制台应用程序之前运行我的析构函数?
- 如何结束图形GUI和执行控制台函数(QT)
- 将函数从控制台应用程序移动到共享库项目似乎会带来不相关的编译错误
- winrt 命名空间是否具有控制台函数?
- 如何在多线程 MEX 函数中打印到控制台?
- 在单元测试(Nunit或MSTest)中从C#中调用的C 函数的原因是从控制台应用程序中运行的同一代码的不同结果
- 我的函数返回的是打印到控制台,它只会在我调用它时每隔一段时间C++
- VBA运行声明的函数但C++控制台应用程序运行正常时堆损坏
- 如何在C++中调用具有参数和用户控制台输入的函数
- 将打印到控制台的函数输出重定向到字符串
- 如何编译使用 PlaySound 函数的 c++ 控制台应用程序
- C/C++中的ClearConsole()-用于清除控制台的函数
- c++控制台程序中strcpy函数的异常.strcpy()中的
- 如何使用C++应用程序和系统函数来执行java控制台应用程序
- 如何从用户(即控制台)提供数组元素,以及如何将数组元素作为参数传递给函数
- 有人能解释一下为什么只有一个函数打印到控制台吗?
- 在控制台执行函数关闭c++
- 如何从Windows Service主函数启动c++本地应用程序(应用程序与控制台交互)?
- 错误LNK2019:函数___tmainCRTStartup中引用了未解析的外部符号_main,但这次不是 Windows/控制台问题!