Home > Software engineering >  Is possible to print variables values to Debug without specifying their types?
Is possible to print variables values to Debug without specifying their types?

Time:09-18

I'm printing the value of variables to DebugView.

There is any 'easier' way to print their value other than manually specifying the % VarTYPE

Currently doing it this way:

WCHAR wsText[255] = L"";
wsprintf(wsText, L"dwExStyle: %??? lpClassName: %??? lpWindowName: %??? ...", dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, ...);
return CreateWindowExA(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);

It doesn't necessarily need to be wsprintf, being able to print it without needing to manually specify each parameter type would help!

CodePudding user response:

Yes use string streams, they're more safe then wsprintf too (buffer overruns). And for unknown types you can overload operator <<.

#include <Windows.h>
#include <string>
#include <sstream>

int main()
{
    DWORD dwExStyle{ 0 };
    std::wstringstream wss;
    wss << L"dwExtStyle : " << dwExStyle << ", lpClassName: "; // and more....
    OutputDebugString(wss.str().c_str());
}

std::ostream& operator<<(std::ostream& os, const your_type& value)
{
    os << value.member; // or something
    return os;
}
  • Related