c++ - 只读内存映射寄存器在 C 中定义为 `volatile const` 但在 C++ 中仅定义为 `volatile`

标签 c++ c embedded cmsis

在使用 Atmel SAM3X8E 处理嵌入式系统项目时,我注意到某些 CMSIS 头文件中有以下代码。

#ifndef __cplusplus
typedef volatile const uint32_t RoReg; /**< Read only 32-bit register (volatile const unsigned int) */
#else
typedef volatile       uint32_t RoReg; /**< Read only 32-bit register (volatile const unsigned int) */
#endif

为什么 C++ 的 typedef 不包含 const?我在某处看到有人提到 C++ 不会在运行时内存中存储整数 const 变量,如果为真,则意味着 const 需要被删除,因为微 Controller 寄存器是如何映射内存的,但我可以'似乎没有找到任何其他说明 C++ 可以做到这一点的内容(尽管我的搜索确实非常简短)。没有太多的 C++ 经验,我还认为可能是 C++ 不允许 const 结构成员,因为这些 typedef 主要用于寄存器集合的 struct typedef,但这似乎不是也是如此。

最佳答案

如果使用 const 声明,C++ 标准将强制您初始化变量的内容。对于微 Controller 寄存器,您不想这样做。

关于c++ - 只读内存映射寄存器在 C 中定义为 `volatile const` 但在 C++ 中仅定义为 `volatile`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15415158/

相关文章:

c++ - 是否可以使用用户定义的 I/O 操纵器来操纵某些文本?

c++ - ‘int main;’ 是有效的 C/C++ 程序吗?

C++ : How to bind inherited methods only to parent classes?

c - 我如何使用此客户端程序访问 tomcat 资源?

c - 调试器对嵌入式应用程序的影响

c++ - 使用 C++11 编译 ui 代码时遇到 "unable to find string literal operator"错误

c - 无效使用灵活数组 - 灵活结构数组作为另一个结构的成员

c - 使用 C 为 atmega328p 微 Controller 设计 react 定时器

android - x-loader ECC错误

c - `sudo` 从 C 以 root 权限连接到 wifi