I need to declare a class object as volatile:
int main() {
volatile FixedQueue<Job, 5> q{};
q.push({ 0x42, 0x55 });
}
I learned from this post that all member functions should be volatile so they can be called from a volatile object:
template <typename T, size_t N>
class FixedQueue {
public:
FixedQueue() = default;
constexpr void push(const T& obj) volatile {
m_back = plusOne(m_back);
*m_back = obj;
}
private:
T* plusOne(T* ptr) volatile {
if (ptr + 1 == m_data + N) return m_data;
return ptr + 1;
}
T m_data[N]{};
T* m_back{ m_data };
};
However, the compiler says invalid conversion from 'volatile Job*' to 'Job*'
, referring to return m_data
, which leads me to believe that m_data is of type volatile T[]
. To deal with this, I change plusOne()'s return type to volatile T*
.
The error reappears but on the line m_back = plusOne(m_back);
, which leads me to believe that m_back is of type volatile T*
. I change plusOne's parameter type to volatile T*
.
The error doesn't go away.
Where is this conversion happening, and what am I doing wrong?
Complete program (note that this is a simplified example of my actual program):
#include <stddef.h>
#include <stdint.h>
template <typename T, size_t N>
class FixedQueue {
public:
FixedQueue() = default;
constexpr void push(const T& obj) volatile {
m_back = plusOne(m_back);
*m_back = obj;
}
private:
volatile T* plusOne(volatile T* ptr) volatile {
if (ptr + 1 == m_data + N) return m_data;
return ptr + 1;
}
T m_data[N]{};
T* m_back{ m_data };
};
struct Job{ uint8_t byte; uint8_t addr; };
int main() {
volatile FixedQueue<Job, 5> q{};
q.push({ 0x42, 0x55 });
}