使用预处理器指令定义美元符号表示的内容是否会引起冲突? [英] Will using a preprocessor directive to define what a dollar sign represents cause any conflicts?

查看:100
本文介绍了使用预处理器指令定义美元符号表示的内容是否会引起冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 C ++ 中使用以下内容吗?:

Can I use the following in C++?:

#define $ cout

int main(){
    $<<"Hello World!\n";
    return 0;
}

我想知道它是否会引起任何冲突.

I'm wondering whether it will cause any conflicts.

推荐答案

这不是绝对合法的,但您的实现可以接受.

It's not definitively legal, but your implementation is allowed to accept it.

考虑:

[C++11: 2.5/1]:每个转换为令牌(2.7)的预处理令牌都应具有关键字,标识符,文字,运算符或标点符号的词法形式.

[C++11: 2.5/1]: Each preprocessing token that is converted to a token (2.7) shall have the lexical form of a keyword, an identifier, a literal, an operator, or a punctuator.

在这里,您的$显然不是关键字,运算符或标点符号(因为它们在标准中已枚举),而且看起来不像文字,因此只能是标识符;现在,标识符必须仅包含字母数字和下划线,并且数字不能前导(基于[C++11: 2.11]下表示的语法).

Here, your $ is obviously not a keyword, operator or punctuator (as these are enumerated in the standard), and it doesn't look like a literal, so it could only be an identifier; now, identifiers must contain only alphanumerics and underscores, and digits cannot be leading (based on the grammar denoted under [C++11: 2.11]).

但是,该标准还允许实现接受 other 个字符,因此您想要执行的操作可能有效,但不能移植.

However, the standard does also allow implementations to accept other characters, so what you want to do may work, but it will not be portable.

这篇关于使用预处理器指令定义美元符号表示的内容是否会引起冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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