Why does std::cout convert volatile pointers to bool?
Posted
by Joseph Garvin
on Stack Overflow
See other posts from Stack Overflow
or by Joseph Garvin
Published on 2010-03-23T16:30:54Z
Indexed on
2010/03/23
16:33 UTC
Read the original article
Hit count: 401
If you try to cout a volatile pointer, even a volatile char pointer where you would normally expect cout to print the string, you will instead simply get '1' (assuming the pointer is not null I think). I assume output stream operator<< is template specialized for volatile pointers, but my question is, why? What use case motivates this behavior?
Example code:
#include <iostream>
#include <cstring>
int main()
{
char x[500];
std::strcpy(x, "Hello world");
int y;
int *z = &y;
std::cout << x << std::endl;
std::cout << (char volatile*)x << std::endl;
std::cout << z << std::endl;
std::cout << (int volatile*)z << std::endl;
return 0;
}
Output:
Hello world
1
0x8046b6c
1
© Stack Overflow or respective owner