未定义的C / C ++符号作为运算符 [英] undefined C/C++ symbol as operator

查看:212
本文介绍了未定义的C / C ++符号作为运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到字符/符号`'和'@'在C / C ++中不用作运算符,


  1. 有没有人知道原因或历史上为什么它的?

  2. 如果它真的没有使用,可以使用#define将这些符号定义为另一个运算符/语句吗?


<通常, #define 只接受宏名称中的有效标识符,因此您不能这样做:

  #define @ at 
#define @(x)[x]

与反向报价类似。你并没有提到'$',这在标识符中有时是允许的。



可能有一个编译器特定的扩展来允许这样的映射,但我不会使用它。






至于历史原因,有一些 ISO 646 字符集,用于国家级字符的国家实施。这些保留部分包括引起麻烦的字符,并且标准C(因此标准C ++)中的三字符和二字图特征分别在1989和1994年被添加到ISO C,以提供问题的解决方法。



Trigraphs



在C89标准化过程中添加了Trigraph,以防止人们看到字母字符(使用斯堪的纳维亚语)在C代码中(改编自B Stroustrup,C ++的设计与演进中的一个例子,使用了丹麦终端):

  #include< stdio.h> 
int main(int argc,char **argvÆÅ)
æ
if(argc <1øøargvÆ1Å=='Ø0')return 0;
printf(Hello,%sØn,argvÆ1Å);
å

或者,在ISO 8859-1代码集(或任何ISO 8859-x代码集):

  #include< stdio.h> 
int main(int argc,char ** argv [])
{
if(argc <1 || argv [1] =='\0')return 0;
printf(Hello,%s\\\
,argv [1]);
}

引入三字母以产生代码的中性格式:

  ?? = include< stdio.h> 
int main(int argc,char ** argv ??(??))
??<
if(argc <1 ??!??!* argv ??(1 ??)=='?? / 0')return 0;
printf(Hello,%s ?? / n,argv ??(1?));
??>

这也不是很可读,但对每个人都是一样的。

  Trigraph相当于
?? / \反斜杠
??< {open brace
??> } close brace
??([方括号
??]] close方括号
?? =#hash(美式英镑,英镑为英镑)
?'^ caret
??! |管道
?? - 〜tilde

标准说没有其他三字母。这就是为什么识别转义序列'\?'(作为一个简单的问号 - 虽然可能是'?? /?')。注意,GNU编译器集合(GCC)不解释三元组,除非你握住它的手(在命令行上指定 -trigraphs ')。



数字



这些有向图在1994年添加,并不像三字母一样普遍或侵入;它们只出现在字符串和字符串字面之外。有形图是:

  Digraph等同于
< ;: [
:> ]
<%{
%> }
%:#
%:%:##

有向图(和三字母):

 %:include< stdio.h> 
%:include< iso646.h>
int main(int argc,char ** argv< ::>)
<%
if(argc< 1或* argv<:1:> =='? ?/ 0')return 0;
printf(Hello,%s ?? / n,argv<:1:>);
%>



特别在签名和背面报价?



如果你看看上面的维基百科URL,你会看到'@'和'`'有时被国家字符替代,因此不是好的标识符。不使用'@'的另一个原因是,在引入C时,'#是默认的擦除字符,'@'是终端的kill(行擦除)字符。因此,你必须记住逃避它们因为'#'只出现在一行的开头,这不是太多的问题(使用'#'和'##'来得太多,以后再次标准化),但'@'会擦掉在这之前的几天,'vi' - 'ed是标准的Unix编辑器。


I notice that the character/symbol '`' and '@' is not used as an operator in C/C++,

  1. does anyone know the reason or historically why its so?
  2. if its really not used, is it safe to define those symbols as another operator/statement using #define?

解决方案

Normally, #define only accepts valid identifiers in the macro name - so you cannot do:

#define @      at
#define @(x)   [x]

Similarly with back-quote. And you didn't mention '$', which is sometimes allowed in identifiers.

There might be a compiler-specific extension to allow such mappings, but I wouldn't use it.


As to the historical reason for this, there are parts of the ISO 646 character set that are reserved to national implementations for national characters. These reserved portions include the characters that cause trouble, and the trigraphs and digraphs features in Standard C (and hence Standard C++) were added to ISO C in 1989 and 1994 respectively to provide workarounds for the problems.

Trigraphs

Trigraphs were added during the C89 standardization process to prevent people from, for example, having to see alphabetic characters (in Scandinavian languages) used in their C code (adapted from an example in B Stroustrup, 'Design and Evolution of C++', using a Danish terminal):

#include <stdio.h>
int main(int argc, char **argvÆÅ)
æ
    if (argc < 1 øø *argvÆ1Å == 'Ø0') return 0;
    printf("Hello, %sØn", argvÆ1Å);
å

Or, in the ISO 8859-1 code set (or any of the ISO 8859-x code sets):

#include <stdio.h>
int main(int argc, char **argv[])
{
     if (argc < 1 || argv[1] == '\0') return 0;
     printf("Hello, %s\n", argv[1]);
}

The trigraphs were introduced to produce a neutral format for the code:

??=include <stdio.h>
int main(int argc, char **argv??(??))
??<
    if (argc < 1 ??!??! *argv??(1??) == '??/0') return 0;
    printf("Hello, %s??/n", argv??(1??));
??>

That's not very readable, either, but it is the same for everyone.

Trigraph      Equivalent to
??/           \      backslash
??<           {      open brace
??>           }      close brace
??(           [      open square bracket
??)           ]      close square bracket
??=           #      hash (pound in American, but a pound is £ in English)
??'           ^      caret
??!           |      pipe
??-           ~      tilde

The standard says 'there are no other trigraphs'. This is why the escape sequence '\?' is recognized (as a simple question mark - though presumably that is '??/?'). Note that the GNU Compiler Collection (GCC) does not interpret trigraphs unless you hold its hand to the fire (specify '-trigraphs' on the command line).

Digraphs

The digraphs were added in 1994, and are not as pervasive or intrusive as trigraphs; they only appear outside strings and string literals. The digraphs are:

Digraph       Equivalent to
<:            [
:>            ]
<%            {
%>            }
%:            #
%:%:          ##

The example using digraphs (and trigraphs):

%:include <stdio.h>
%:include <iso646.h>
int main(int argc, char **argv<::>)
<%
    if (argc < 1 or *argv<:1:> == '??/0') return 0;
    printf("Hello, %s??/n", argv<:1:>);
%>

At sign and back quote specifically?

If you look at the Wikipedia URL above, you'll see that both '@' and '`' are sometimes replaced by national characters - and hence not good identifiers. An additional reason for not using '@' is that at the time C was introduced, '#" was the default erase character and '@' was the kill (line erase) character for terminals. So, you had to remember to escape them. Since '#' only appeared at the beginning of a line, it wasn't too much of a problem (using '#' and '##' came much, much later - standardization again), but '@' would have wiped out all the preceding typing on the line. And this is the days before 'vi' - 'ed is the the standard Unix editor'.

这篇关于未定义的C / C ++符号作为运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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