c++ - Why is it OK to initialize global variable with result from inline assembly code?
问题描述
Consider this code which would compile on PIC32 (source: openscope-mz)
static uint32_t tSLoop = ReadCoreTimer();
static inline uint32_t ReadCoreTimer(void)
{
uint32_t coreTimerCount;
__asm__ __volatile__("mfc0 %0,$9" : "=r" (coreTimerCount));
return(coreTimerCount);
}
Please explain why this is valid C code.
解决方案
不,这是没有实现定义扩展的无效 C 代码。静态变量的初始化必须是常量表达式。它指定了什么是常量表达式 - 函数的结果不在该列表中。C 标准允许实现接受其他形式的常量表达式,因此通过实现定义的扩展来接受此类语句,它可能是有效的 C 代码。无论如何,我怀疑情况并非如此。
线
static uint32_t tSLoop = ReadCoreTimer();
来自LoopStats.cpp,它是一个 C++ 文件。
推荐阅读
- c# - 当 IDENTITY_INSERT 设置为 OFF 时,无法在表“TABLE”中插入标识列的显式值 异常更新我的表
- bash - 使用 curl 登录 php 站点
- vue.js - Vue - 从一个组件访问属性到另一个组件
- c++ - 为什么 if 语句忽略命令行参数?
- python - 在我需要对另一个数据帧进行子集化的数据帧上进行矢量化操作(皮尔逊相关性)
- python - 带有自定义训练步骤的 TensorFlow 分布式训练
- java - Spring MVC - 从 yaml 文件中注入映射
- spring-boot - 使用 Spring 微服务禁止其他用户看不到他们的数据
- delphi - 克隆复杂对象的简单方法
- css - 如果选择器 :out-of-range 小于或大于输入范围,我如何检查 css?