Type of a C++ string literal

后端 未结 3 954
攒了一身酷
攒了一身酷 2020-12-19 10:28

Out of curiosity, I\'m wondering what the real underlying type of a C++ string literal is.

Depending on what I observe, I get different results.

A typeid tes

3条回答
  •  北海茫月
    2020-12-19 10:50

    The type of a string literal is indeed const char[SIZE] where SIZE is the length of the string plus the null terminating character.

    The fact that you're sometimes seeing const char* is because of the usual array-to-pointer decay.

    But I don't see how it could be const char * as the following line is accepted by VS12: char* s = "Hello";

    This was correct behaviour in C++03 (as an exception to the usual const-correctness rules) but it has been deprecated since. A C++11 compliant compiler should not accept that code.

提交回复
热议问题