Upraviť

Zdieľať cez


Warning C6392

This expression writes the value of the pointer to the stream. If this is intentional, add an explicit cast to 'void *'

This rule was added in Visual Studio 2022 17.8.

Remarks

C++ supports wide character streams such as std::wostringstream, and nonwide character streams such as std::ostringstream. Trying to print a wide string to a nonwide stream calls the void* overload of operator<<. This overload prints the address of the wide string instead of the value.

Code analysis name: STREAM_OUTPUT_VOID_PTR

Example

The following code snippet prints the value of the pointer to the standard output instead of the string "Pear":

#include <iostream>

int main() {
    std::cout << L"Pear\n"; // Warning: C6392
}

There are multiple ways to fix this error. If printing the pointer value is unintended, use a nonwide string:

#include <iostream>

int main() {
    std::cout << "Pear\n"; // No warning.
}

Alternatively, use a wide stream:

#include <iostream>

int main() {
    std::wcout << L"Pear\n"; // No warning.
}

If the behavior is intentional, make the intention explicit and silence the warning by using an explicit cast:

#include <iostream>

int main() {
    std::cout << static_cast<void*>(L"Pear\n"); // No warning.
}