C 和 C++ 中的多字符文字 [英] Multicharacter literal in C and C++

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

问题描述

我不知道 C 和 C++ 允许 多字符文字:不是 'c'(在 C 和 中属于 int 类型C++ 中的 char),但是 'tralivali'(int 类型!)

I didn't know that C and C++ allow multicharacter literal: not 'c' (of type int in C and char in C++), but 'tralivali' (of type int!)

enum
{
    ActionLeft = 'left',
    ActionRight = 'right',
    ActionForward = 'forward',
    ActionBackward = 'backward'
};

标准说:

C99 6.4.4.4p10:一个整型字符常量包含多个字符(例如,'ab'),或包含字符或转义不映射到的序列单字节执行字符,是实现定义."

C99 6.4.4.4p10: "The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined."

我发现它们被广泛用于 C4 引擎.但是我想当我们谈论与平台无关的序列化时,它们并不安全.Thay 也可能令人困惑,因为它看起来像字符串.那么多字符文字的使用范围是什么,它们对某些东西有用吗?它们在 C++ 中只是为了与 C 代码兼容吗?它们是否被认为是 goto 运算符的不良功能?

I found they are widely used in C4 engine. But I suppose they are not safe when we are talking about platform-independend serialization. Thay can be confusing also because look like strings. So what is multicharacter literal's scope of usage, are they useful for something? Are they in C++ just for compatibility with C code? Are they considered to be a bad feature as goto operator or not?

推荐答案

我不知道它的使用范围有多广,但是实现定义"对我来说是一个很大的危险信号.据我所知,这可能意味着实现可以选择忽略您的字符名称,并根据需要仅分配正常的增量值.它可能会做一些更好"的事情,但你不能依赖编译器(甚至编译器版本)之间的这种行为.至少转到"具有可预测(如果不受欢迎)的行为...

I don't know how extensively this is used, but "implementation-defined" is a big red-flag to me. As far as I know, this could mean that the implementation could choose to ignore your character designations and just assign normal incrementing values if it wanted. It may do something "nicer", but you can't rely on that behavior across compilers (or even compiler versions). At least "goto" has predictable (if undesirable) behavior...

无论如何,这是我的 2c.

That's my 2c, anyway.

关于实现定义":

来自 Bjarne Stroustrup 的 C++ 词汇表:

实现定义 - C++ 语义的一个方面,为每个实现定义,而不是在标准中为每个实现指定.一个例子是 int 的大小(它必须至少为 16 位,但可以更长).尽可能避免实现定义的行为.另见:未定义.TC++PL C.2.

implementation defined - an aspect of C++'s semantics that is defined for each implementation rather than specified in the standard for every implementation. An example is the size of an int (which must be at least 16 bits but can be longer). Avoid implementation defined behavior whenever possible. See also: undefined. TC++PL C.2.

还有...

undefined - C++ 语义的一个方面,不需要合理的行为.一个示例是取消引用值为零的指针.避免未定义的行为.另见:实现定义.TC++PL C.2.

undefined - an aspect of C++'s semantics for which no reasonable behavior is required. An example is dereferencing a pointer with the value zero. Avoid undefined behavior. See also: implementation defined. TC++PL C.2.

我相信这意味着评论是正确的:它至少应该编译,尽管没有指定超出的任何内容.还要注意定义中的建议.

I believe this means the comment is correct: it should at least compile, although anything beyond that is not specified. Note the advice in the definition, also.

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

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