简体   繁体   English

从 volatile 结构中索引元素在 C++ 中不起作用

[英]indexing an element from a volatile struct doesn't work in C++

I have this code:我有这个代码:

typedef struct {
    int test;
} SensorData_t;
volatile SensorData_t sensorData[10];

SensorData_t getNextSensorData(int i)
{
    SensorData_t data = sensorData[i];
    return data;
}


int main(int argc, char** argv) {

}

It compiles with gcc version 8.3, but not with g++.它可以用 gcc 8.3 版编译,但不能用 g++ 编译。 Error message:错误信息:

main.c: In function ‘SensorData_t getNextSensorData(int)’:
main.c:8:34: error: no matching function for call to ‘SensorData_t(volatile SensorData_t&)’
  SensorData_t data = sensorData[i];
                                  ^
main.c:3:3: note: candidate: ‘constexpr SensorData_t::SensorData_t(const SensorData_t&)’ <near match>
 } SensorData_t;
   ^~~~~~~~~~~~
main.c:3:3: note:   conversion of argument 1 would be ill-formed:
main.c:8:34: error: binding reference of type ‘const SensorData_t&’ to ‘volatile SensorData_t’ discards qualifiers
  SensorData_t data = sensorData[i];
                      ~~~~~~~~~~~~^
main.c:3:3: note: candidate: ‘constexpr SensorData_t::SensorData_t(SensorData_t&&)’ <near match>
 } SensorData_t;
   ^~~~~~~~~~~~
main.c:3:3: note:   conversion of argument 1 would be ill-formed:
main.c:8:34: error: cannot bind rvalue reference of type ‘SensorData_t&&’ to lvalue of type ‘volatile SensorData_t’
  SensorData_t data = sensorData[i];

I'm not sure if I need to add volatile as well for the data variable and the return type, shouldn't be needed because it is copied.我不确定是否需要为data变量和返回类型添加 volatile,因为它是复制的,所以不需要。 But I do access the sensorData array from an interrupt as well (on an embedded system), so I think I need volatile for the top level variable sensorData.但是我也确实从中断(在嵌入式系统上)访问了 sensorData 数组,所以我认为我需要volatile用于顶级变量 sensorData。

Your program is trying to copy a SensorData_t object.您的程序正在尝试复制SensorData_t对象。 The compiler supplies a copy constructor with the following signature:编译器提供具有以下签名的复制构造函数:

SensorData_t(const SensorData_t &)

This copy constructor will not work with volatile arguments, hence the compilation error.此复制构造函数不适用于volatile参数,因此会出现编译错误。

You can write your own copy constructor which works with volatile SensorData_t objects (as well as non- volatile SensorData_t objects):您可以编写自己的复制构造函数,它适用于volatile SensorData_t对象(以及volatile SensorData_t对象):

struct SensorData_t {
    SensorData_t() = default;

    SensorData_t(const volatile SensorData_t &other)
        : test(other.test) {
    }

    int test;
};

As an alternative to the accepted answer, you may consider avoid copying the data from the array altogether, if the getNextSensorData is a simple getter function for the global sensorData :作为已接受答案的替代方案,如果getNextSensorData是全局sensorData的简单getter函数,您可以考虑完全避免从数组中复制数据:

// Intorduce type alias
using SD = const volatile SensorData_t&;

SD getNextSensorData(int i)
{
    return sensorData[i];
}

This way (if it is suitable for the design), you'll keep the definitions code more concise.这样(如果它适合设计),您将保持定义代码更简洁。 But I have to note that the usage is a bit less direct than in the copy case:但我必须注意,它的用法比在复制情况下要不那么直接:

int main() {
    // Type alias
    SD sd1 = getNextSensorData(0);
    // Deduced type
    auto&& sd2 = getNextSensorData(1);
    // Explicit type
    const volatile SensorData_t& sd3 = getNextSensorData(2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM