我什么时候会在 C++ 中使用 const volatile、register volatile、static volatile?

When would I use const volatile, register volatile, static volatile in C++?(我什么时候会在 C++ 中使用 const volatile、register volatile、static volatile?)

本文介绍了我什么时候会在 C++ 中使用 const volatile、register volatile、static volatile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 volatile 关键字与 registerconststatic 的不同用法关键词.我不确定有什么影响,所以我认为:

I am wondering about the different uses of the volatile keyword in combination with register, const and static keywords. I am not sure what are the effects, so I think:

register volatile int T=10;

建议编译器将 T 存储在寄存器中,并且可以从外部(操作系统、硬件、另一个线程)修改 T 的值

Suggest the compiler to store T in a register and the value of T can be modified from somewhere outside (OS, hardware, another thread)

const volatile int T=10;

程序本身不能修改T,但可以在代码之外的某处修改T.

The program itself can not modify T, but T can be modified frow somewhere outside the code.

static volatile int T=10;

如果 T 是类的数据成员,则意味着该类的所有对象都具有相同的 T 值,并且可以从外部修改 T.如果 T 是文件中的全局变量,则其他文件(属于项目的一部分)中的源代码无法访问 T,但可以从外部某处访问 T.如果 T 是函数中的局部变量,一旦被初始化,它就会留在内存中直到程序结束,并且可以从外面的某个地方修改.

If T is a data member of a class it means that all the objects of the class have the same value for T and T can be modified from somewhere outside. If T is a global variable in a file, the source code in other files (that are part of the project) cannot access T, but T can be accessed from somewhere outside. If T is a local variable in a function,once it has been initialized remains in the memory until the end of the program and can be modified from somewhere outside.

我的想法是否正确,任何有经验的 C++ 开发人员都可以举一个例子,说明上述内容可能在实际应用程序中使用还是非常罕见?

Are my thoughts correct and can any experienced C++ developer give an example where the above maybe used in real-world applications or it is very rare?

推荐答案

register volatile int T=10;

volatile 限定符意味着编译器不能对 T 应用优化或重新排序访问,而 register 是对编译器的提示 T 将被大量使用.如果取T 的地址,则编译器会简单地忽略该提示.请注意,register 已弃用但仍在使用.

volatile qualifier means that the compiler cannot apply optimizations or reorder access to T, While register is a hint to the compiler that T will be heavily used. If address of T is taken, the hint is simply ignored by the compiler. Note that register is deprecated but still used.

实际使用:

我从来没有用过它,从来没有觉得需要它,现在也想不出任何东西.

I have never used it never felt the need for it and can't really think of any right now.

const volatile int T=10;

const 限定符表示不能通过代码修改T.如果您尝试这样做,编译器将提供诊断信息.volatile 仍与情况 1 的含义相同.编译器无法优化或重新排序对 T 的访问.

const qualifier means that the T cannot be modified through code. If you attempt to do so the compiler will provide a diagnostic. volatile still means the same as in case 1. The compiler cannot optimize or reorder access to T.

实际使用:

  • 以只读模式访问共享内存.
  • 以只读模式访问硬件寄存器.
static volatile int T=10;

static 存储限定符给出 T 静态存储持续时间(C++11 §3.7)和 内部链接,而 volatile 仍然控制优化和重新排序.

static storage qualifier gives T static storage duration (C++11 §3.7) and internal linkage, while volatile still governs the optimization and reordering.

实际使用:

  • volatile 相同,但您需要对象具有静态存储持续时间并且其他翻译单元无法访问.
  • Same as volatile except that you need the object to have static storage duration and to be inaccessible from other translation units.

这篇关于我什么时候会在 C++ 中使用 const volatile、register volatile、static volatile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我什么时候会在 C++ 中使用 const volatile、register volatile、static volatile?

基础教程推荐