为什么指针没有隐式转换成<type> const*const

Why is the pointer not implicitly converted into <type> const*const

AT45Status 是一个有几个成员的结构,我有以下功能:

/**
 * @brief read the chip status from the flash chip
 * @retval a pointer to the current status of the chip
 */
AT45Status const * const ReadChipStatus(void) {
  uint8_t commands[1] = {STATUS_READ};
  static AT45Status status;

  //// … details, but the result is updated values inside status

  return &status;
}

如果我使用 C++ 编译它,我会收到以下警告:

…(16): warning: #815-D: type qualifier on return type is meaningless

我当然查找了 an exact explanation,它是一个针对语义传递值的普通类型的相关警告。但是我正在 return 指向一个我不想更改的对象。 (指针或指向的数据)。因此,类型限定符是有意义的。

当我将 return 语句更改为:

时警告消失
return (AT45Status const * const)&status;

为什么编译器不能t/doesn隐式地将 AT45Status* 转换为 AT45Status const * const?我在这次互动中错过了什么?

例如这样的声明

const int f();

对基本类型没有意义。返回值是一个临时对象,即使您删除 const 限定符也无法更改。

int f();

例如你可能不会写

++f();

编译器会报错。

同样的情况也发生在指针上。

这个警告是想告诉你你正试图return一个const指针,这是没有意义的,因为returned指针本身是不能被修改的.

另一方面,将被指向的对象限定为 const 是有意义的(使指向的对象不可修改),即 return 指向 const 的指针。例如

AT45Status const * ReadChipStatus(void)