C和C之间的字符串字面量的差异++ [英] String Literal Differences Between C and C++

查看:116
本文介绍了C和C之间的字符串字面量的差异++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,之前C ++ 11,字符串中几乎完全C和C之间以同样的方式共办理++。

As far as I can tell, before C++11, string literals were handled in almost exactly the same way between C and C++.

现在,我承认,在宽字符串文字的处理C和C ++之间的差异。

Now, I acknowledge that there are differences between C and C++ in the handling of wide string literals.

这是我能找到的唯一区别是在一个阵列由字符串初始化。

The only differences that I have been able to find are in the initialization of an array by string literal.

char str[3] = "abc"; /* OK in C but not in C++ */
char str[4] = "abc"; /* OK in C and in C++. Terminating zero at str[3] */

和技术不同的是只在C ++的问题。在C ++ ABC为const char [4] ,而在C中,它是的char [ 4] 。不过,C ++有一个特殊的规则,允许转换为为const char * 再到的char * 来留住C兼容性直到C ++ 11时,不再适用的特殊规则。

And a technical difference that only matters in C++. In C++ "abc" is const char [4] while in C it is char [4]. However, C++ has a special rule that allows the conversion to const char * and then to char * to retain C compatibility up until C++11 when that special rule is no longer applied.

和在文字的允许长度的差。然而,作为一个实际问题是任何编译编译C和C ++ code将不执行低级C限制。

And a difference in allowed lengths of literals. However, as a practical matter any compiler that compiles both C and C++ code will not enforce the lower C limit.

我有申请一些相关链接:

I have some interesting links that apply:

  • http://david.tribble.com/text/cdiffs.htm
  • http://www.coding-guidelines.com/cbook/c90c++.pdf

是否有任何的其他的区别在哪里?

Are there any other differences?

推荐答案

有一个明显的区别是,C ++的字符串是C的人的一个超集。特别是C ++现在支持原始字符串(的not用C支持),在技术上§2.14.15定义,一般在HTML和XML用在经常遇到的。

Raw strings

A noticeable difference is that C++'s string literals are a superset of C ones. Specifically C++ now supports raw strings (not supported in C), technically defined at §2.14.15 and generally used in HTML and XML where " is often encountered.

原始字符串允许您指定的形式自己的分隔符(最多16个字符):

Raw strings allow you to specify your own delimiter (up to 16 characters) in the form:

R"delimiter(char sequence)delimiter"

这是特别有用,通过提供自己的字符串分隔符,以避免不必要的转义字符。下面的两个例子告诉你如何避免逃逸分别为:

This is particularly useful to avoid unnecessary escaping characters by providing your own string delimiter. The following two examples show how you can avoid escaping of " and ( respectively:

std::cout << R"(a"b"c")";      // empty delimiter
std::cout << '\n';
std::cout << R"aa(a("b"))aa";  // aa delimiter
// a"b"c"
// a("b")

<大骨节病>现场演示

另一个区别,在评论中指出的那样,是字符串文字具有类型的char [N] 在C,截至§6.4.5/ 6条规定:

Another difference, pointed out in the comments, is that string literals have type char [n] in C, as specified at §6.4.5/6:

有关字符串常量,数组元素具有char类型,并与多字节字符序列的各个字节被初始化。

For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence.

而在C ++他们键入为const char [N] ,如§2.14.5/ 8定义的:

while in C++ they have type const char [n], as defined in §2.14.5/8:

普通字符串文字和UTF-8字符串文字也称为窄字符串。一个箭头
  字符串的类型,其中n是定义如下字符串的大小N为const char数组,并有
  静态存储持续时间(3.7)。

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. 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).

这并没有改变,在这两个标准(在§6.4.5/ 7和C和C ++分别2.14.5 / 13)试图在不确定的行为来修改字符串的结果事实。

This doesn't change the fact that in both standard (at §6.4.5/7 and 2.14.5/13 for C and C++ respectively) attempting to modify a string literal results in undefined behavior.

另一个细微的差别是,在C,阉字符串的字符数组不同的是不确定的,因为每§6.4.5/ 7:

Another subtle difference is that in C, wether the character arrays of string literals are different is unspecified, as per §6.4.5/7:

这是不确定的,这些阵列是否提供了不同的元素具有适当的值。

It is unspecified whether these arrays are distinct provided their elements have the appropriate values.

而在C ++中这是实现定义,按照§2.14.5/ 13:

while in C++ this is implementation defined, as per §2.14.5/13:

不管所有的字符串文字是不同的(也就是说,存储在非重叠的对象)是实现定义。

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation- defined.

这篇关于C和C之间的字符串字面量的差异++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆