为什么创建了register关键字? [英] Why was the register keyword created?

查看:57
本文介绍了为什么创建了register关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读不是(或, :我遇到了以下几行:

While reading Keywords That Aren't (or, Comments by Another Name) by Herb Sutter I came across these lines:

是的,有些关键字在语义上等同于空格,是引人注目的注释.

That's right, some keywords are semantically equivalent to whitespace, a glorified comment.

还有

我们已经了解了C ++语言为什么将关键字视为保留字,并且已经看到了两个关键字(自动和注册),这些关键字对C ++程序没有任何语义上的区别.不要使用它们;无论如何,它们只是空白,并且有更快的键入空白的方法.

We've seen why the C++ language treats keywords as reserved words, and we've seen two keywords —auto and register — that make no semantic difference whatsoever to a C++ program. Don't use them; they're just whitespace anyway, and there are faster ways to type whitespace.

如果像 auto (在C ++ 11中可能不是这样)和 register 之类的关键字没有价值,那么为什么要创建和使用它们呢?

If the keywords like auto(maybe not in C++11) and register are of no value, then why were they created and used?

如果在变量之前包含 register 并没有什么区别

If it doesn't make any difference to include the register before a variable

#include<stdio.h>
int main(){
    register int a = 15;
    printf("%d\n%d\n",&a,a);
    return 0;
}

上面的程序为什么会报错?

Why does the above program give an error?

test_register.c:在"main"函数中:

test_register.c: In function ‘main’:

test_register.c:4:2:错误:请求了寄存器变量"a"的地址

test_register.c:4:2: error: address of register variable ‘a’ requested

printf(%d \ n%d \ n",& a,a);

printf("%d\n%d\n",&a,a);

以下程序可在C ++中工作.

The following program works in C++.

#include<iostream>
int main(){
    register int a = 15;
    std::cout<<&a<<'\n'<<a;
    return 0;
}

推荐答案

注册

在C语言中, register 存储类用作编译器的提示,以表示

register

In C, the register storage class was used as a hint to the compiler, to express that a variable should be preferentially stored in a register. Note that the hint to store a register variable in an actual register may or may not be honored, but in either case the relevant restrictions still apply. See C11, 6.7.1p6 (emphasis mine):

使用存储类说明符 register 声明对象的标识符表明,对对象的访问应尽可能快.这些建议在多大程度上有效取决于实施. [脚注121]

A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined.[footnote 121]

[脚注121]该实现可以将任何 register 声明简单地视为 auto 声明.但是,无论是否实际使用可寻址存储,都无法显式地(通过使用6.5.3.2中讨论的一元&运算符)或隐式(通过将数组名称转换为6.3.2.1中讨论的指针).因此,唯一可以应用于用存储类说明符 register 声明的数组的运算符是 sizeof _Alignof .

[footnote 121] The implementation may treat any register declaration simply as an auto declaration. However, whether or not addressable storage is actually used, the address of any part of an object declared with storage-class specifier register cannot be computed, either explicitly (by use of the unary & operator as discussed in 6.5.3.2) or implicitly (by converting an array name to a pointer as discussed in 6.3.2.1). Thus, the only operators that can be applied to an array declared with storage-class specifier register are sizeof and _Alignof.

在C ++中,它只是一个未使用的保留关键字,但是可以合理地假设保留该关键字是为了与C代码在语法上兼容.

In C++ it is simply an unused reserved keyword, but it's reasonable to assume that it was kept for syntactical compatibility with C code.

在C中, auto 存储类定义了自动存储的变量,但是由于

In C, the auto storage class defines a variable of automatic storage, but it's not usually used since function-local variables are auto by default.

类似地,可以合理地假定它最初只是出于语法兼容性而被移植到C ++,尽管后来它有了自己的含义(

Similarly, it's reasonable to assume that it was initially carried over to C++ for syntactical compatibility only, although later it got its own meaning (type inference).

这篇关于为什么创建了register关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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