Why does std::cout convert volatile pointers to bool?

前端 未结 4 1436
半阙折子戏
半阙折子戏 2020-11-27 22:50

If you try to cout a pointer to a volatile type, even a volatile char pointer where you would normally expect cout to print the string, you will instead simply get \'1\' (as

4条回答
  •  甜味超标
    2020-11-27 23:14

    I think the reason is that volatile pointers cannot be converted implicitly to void *. This is in Appendix C of the Standard, and the rationale is type safety.

    Change: Only pointers to non-const and non-volatile objects may be implicitly converted to void* Rationale: This improves type safety.

    So instead of the conversion to void * (which would print in hex), you get the "default" conversion to bool.

提交回复
热议问题