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

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

问题描述

我不知道C和C ++允许

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?

推荐答案

我不知道这种用法的使用范围,但是实现定义的"对我来说是一个很大的危险信号.据我所知,这可能意味着该实现可以选择忽略您的字符名称,并在需要时仅分配常规的增量值.它可能做些更小"的事情,但是您不能依赖于整个编译器(甚至编译器版本)的行为.至少"goto"具有可预测的(如果不希望的话)行为...

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.

在实现定义"上:

来自 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.

还...

未定义-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天全站免登陆