为什么三角图在现代C ++编译器中产生错误? [英] Why are trigraphs generating errors in modern C++ compilers?

查看:161
本文介绍了为什么三角图在现代C ++编译器中产生错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在GCC编译器中,如果没有明确指定trigraph属性,三字母也不会被编译。

Even in the GCC Compilers, the trigraphs are not getting compiled without explicitly specifying the trigraph attribute.

#include<stdio.h>

int main()
 {
 int a=4;
 if((a==4) ??! (a==5))
   printf("\nHello world!");
 return 0;
 }

这个保存为try.c的程序只有在指定 gcc -Wall -trigraphs try.c ,它仍显示警告。

This program saved as try.c gets compiled in GCC Compiler only when we specify gcc -Wall -trigraphs try.c and it still shows warnings. Can you enlist some compilers that will treat and handle trigraphs without any errors or warnings?

推荐答案

可以使用一些编译器来处理和处理三字母,而不会出现任何错误或警告。 1989年的ANSI C标准,并保留在所有后来的C标准。它们还出现在1998年发布的第一个ISO C ++标准中,以及所有后来的C ++标准(最高达C ++ 14)中。 (Trigraphs将在C ++ 17中删除。感谢Jonathan Leffler和dyp跟踪细节。)

Trigraphs were introduced by the 1989 ANSI C standard, and are retained in all later C standards. They also appear in the first ISO C++ standard, published in 1998, and in all later C++ standards up to C++14. (Trigraphs will be removed in C++17. Thanks to Jonathan Leffler and dyp for tracking down the details.)

它们不是任何一种语言的可选特性;

They are not an optional feature in either language; all conforming compilers must support them and interpret them as specified by the respective language standard.

例如,如果这个程序:

#include <stdio.h>
int main(void) {
    if ('|' == '??!') {
        puts("ok");
    }
    else {
        puts("oops");
    }
    return 0;
}

列印 oops 那么你的编译器是不符合的。

prints oops, then your compiler is non-conforming.

但许多,也许大多数,C编译器默认不完全符合。只要编译器能够以某种方式符合标准,就标准而言就足够了。 (gcc需要 -pedantic -std = ... 才能执行此操作。)

But many, perhaps most, C compilers are not fully conforming by default. As long as a compiler can be made to conform to the standard in some way, that's good enough as far as the standard is concerned. (gcc requires -pedantic and -std=... to do this.)

但是,即使编译器是完全符合的,在标准中没有什么,禁止编译器警告任何它喜欢。合格的C编译器必须诊断任何违反语法规则或约束的行为,但是它可以发出与其喜欢一样多的其他警告,并且不需要区分所需的诊断和其他警告。

But even if a compiler is fully conforming, there's nothing in the standard that forbids a compiler from warning about anything it likes. A conforming C compiler must diagnose any violation of a syntax rule or constraint, but it can issue as many additional warnings as it likes -- and it needn't distinguish between required diagnostics and other warnings.

Trigraphs很少使用。绝大多数开发系统直接支持所有用三叉代替的字符: [ \ ] ^ / code>, | }

Trigraphs are very rarely used. The vast majority of development systems support directly all the characters for which trigraphs substitute: #, [, \, ], ^, {, |, }, ~.

事实上,三脚图很可能被不正确地使用。

In fact, it's likely that trigraphs are used accidentally more often than they're used correctly:

fprintf(stderr, "What just happened here??!\n");

警告有关三字母可能会改变程序的含义(相对于语言没有三字母)是ISO标准和IMHO完全合理的。

Warning about trigraphs that might alter the meaning of a program (relative to the meaning it would have if the language didn't have trigraphs) is both permitted by the ISO standard and IMHO perfectly reasonable. Most compilers probably have options to turn off such warnings.

相反,对于不会实现三字母的C ++ 17编译器,它可能有选项关闭这样的警告。将合理地警告在C ++ 14或更早版本中被视为三字母的序列。同样,禁用此类警告的选项将是一件好事。

Conversely, for a C++17 compiler that doesn't implement trigraphs, it would be reasonable to warn about sequences that would have been treated as trigraphs in C++14 or earlier. Again, an option to disable such warnings would be a good thing.

这篇关于为什么三角图在现代C ++编译器中产生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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