gpt4 book ai didi

c++ - C volatile 位域结构的复制构造函数

转载 作者:搜寻专家 更新时间:2023-10-31 00:09:02 25 4
gpt4 key购买 nike

美好的一天

我正在尝试在 C++11 项目中使用 C SD 驱动程序/文件系统库 (Keil MDK)。它是由 Pack 管理器在 Keil MDK 5.23 中添加的。我正在使用 ARMCC 5.06u4 进行编译

我收到警告 class "_ARM_MCI_STATUS" has no suitable copy constructor"这很奇怪,因为声明它的 header 有 extern "C"{

默认情况下,该包没有将其设置为 C 或 C++ 的选项,但我已将文件手动添加为 C 文件。还是个问题。

该结构在 extern "C"{ 中声明为:

typedef volatile struct _ARM_MCI_STATUS {
uint32_t command_active : 1; ///< Command active flag
uint32_t command_timeout : 1; ///< Command timeout flag (cleared on start of next command)
uint32_t command_error : 1; ///< Command error flag (cleared on start of next command)
uint32_t transfer_active : 1; ///< Transfer active flag
uint32_t transfer_timeout : 1; ///< Transfer timeout flag (cleared on start of next command)
uint32_t transfer_error : 1; ///< Transfer error flag (cleared on start of next command)
uint32_t sdio_interrupt : 1; ///< SD I/O Interrupt flag (cleared on start of monitoring)
uint32_t ccs : 1; ///< CCS flag (cleared on start of next command)
uint32_t reserved : 24;
} ARM_MCI_STATUS;

当结构返回时出现问题:

static ARM_MCI_STATUS GetStatus (MCI_RESOURCES *mci) {
return mci->info->status;
}

其中 status 声明为 ARM_MCI_STATUS status;。我不明白为什么这会成为一个问题。

如果我在没有 --cpp 的情况下编译,那么它编译没有问题。

有什么建议吗?

最佳答案

仅仅因为您的struct 被标记为extern "C" 并不意味着它不会仍然被编译为C++ 代码。

这意味着 return mci->info->status; 调用隐式生成的复制构造函数。因为 _ARM_MCI_STATUS 被标记为 volatile,它的成员是,这意味着采用 T& 的默认复制构造函数不能绑定(bind)到它的 volatile 左值引用通过。

这在 cppreference explanation 中有解释:

Otherwise, the implicitly-declared copy constructor is T::T(T&). (Note that due to these rules, the implicitly-declared copy constructor cannot bind to a volatile lvalue argument.)

而且在实际标准中(只是很难找到正确的条款,但它在那里)。

关于c++ - C volatile 位域结构的复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45300087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com