简体   繁体   English

C ++字符串文字的类型

[英]Type of a C++ string literal

Out of curiosity, I'm wondering what the real underlying type of a C++ string literal is. 出于好奇,我想知道C ++字符串文字的真正底层类型是什么。

Depending on what I observe, I get different results. 根据我观察到的情况,我会得到不同的结果。

A typeid test like the following: 一个类似于以下的类型测试:

std::cout << typeid("test").name() << std::endl;

shows me char const[5] . 给我看char const[5]

Trying to assign a string literal to an incompatible type like so (to see the given error): 尝试将字符串文字分配给不兼容的类型,如此(查看给定的错误):

wchar_t* s = "hello";

I get a value of type "const char *" cannot be used to initialize an entity of type "wchar_t *" from VS12's IntelliSense. 我得到a value of type "const char *" cannot be used to initialize an entity of type "wchar_t *"从VS12的IntelliSense a value of type "const char *" cannot be used to initialize an entity of type "wchar_t *"

But I don't see how it could be const char * as the following line is accepted by VS12: 但我不知道它是如何成为const char *因为VS12接受以下行:

char* s = "Hello";

I have read that this was allowed in pre-C++11 standards as it was for retro-compatibility with C, although modification of s would result in Undefined Behavior. 我已经读过,这在C ++ 11之前的标准中是允许的,因为它与C的复古兼容性,尽管s修改会导致Undefined Behavior。 I assume that this is simply VS12 having not yet implemented all of the C++11 standard and that this line would normally result in an error. 我假设这只是VS12还没有实现所有的C ++ 11标准,并且这条线通常会导致错误。

Reading the C99 standard ( from here , 6.4.5.5) suggests that it should be an array: 阅读C99标准( 从这里 ,6.4.5.5)表明它应该是一个数组:

The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. 然后使用多字节字符序列初始化静态存储持续时间和长度的数组 ,这足以包含序列。

So, what is the type underneath a C++ string literal? 那么, C ++字符串文字下面的类型是什么?

Thank you very much for your precious time. 非常感谢你宝贵的时间。

The type of a string literal is indeed const char[SIZE] where SIZE is the length of the string plus the null terminating character. 字符串文字的类型确实是const char[SIZE] ,其中SIZE是字符串的长度加上空终止字符。

The fact that you're sometimes seeing const char* is because of the usual array-to-pointer decay. 你有时看到const char*的事实是因为通常的数组到指针衰减。

But I don't see how it could be const char * as the following line is accepted by VS12: char* s = "Hello"; 但我不知道它是如何成为const char *因为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. 这是C ++ 03中的正确行为(作为通常的const-correctness规则的一个例外),但它已被弃用。 A C++11 compliant compiler should not accept that code. 符合C ++ 11的编译器不应接受该代码。

The type of a string literal is char const[N] where N is the number of characters including the terminating null character. 字符串文字的类型是char const[N] ,其中N是包括终止空字符的字符数。 Although this type does not convert to char* , the C++ standard includes a clause allowing assignments of string literal to char* . 尽管这种类型不能转换为char*时,C ++标准包括一个条款,允许字符串文本的分配char* This clause was added to support compatibility especially for C code which didn't have const back then. 添加此子句是为了支持兼容性,特别是对于当时没有const C代码。

The relevant clause for the type in the standard is 2.14.5 [lex.string] paragraph 8: 标准中类型的相关条款是2.14.5 [lex.string]第8段:

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. 普通字符串文字和UTF-8字符串文字也称为窄字符串文字。 A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7). 窄字符串文字的类型为“n const char数组”,其中n是下面定义的字符串大小,并且具有静态存储持续时间(3.7)。

First off, the type of a C++ string literal is an array of n const char . 首先,C ++字符串文字的类型是n const char的数组。 Secondly, if you want to initialise a wchar_t with a string literal you have to code: 其次,如果要使用字符串文字初始化wchar_t,则必须编写代码:

wchar_t* s = L"hello"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM