In the following case I try to cast a pointer to a struct into a pointer to another struct (which has the same memory layout behind the scenes). I try to do this in a const correct way, however the compiler is complaining.
I looked at a similar issue but I need the constness to propagate and it doesn't work like described for me.
struct queue
{
// ...
};
typedef struct queue* queue_handle;
struct dummy_queue
{
// ...
};
struct queue_wrapper
{
auto get_queue() const -> queue_handle {
return reinterpret_cast<const queue_handle>(&d);
}
dummy_queue d;
};
int main()
{
queue_wrapper w;
w.get_queue();
}
Error:
<source>: In member function 'queue* queue_wrapper::get_queue() const':
<source>:17:16: error: 'reinterpret_cast' from type 'const dummy_queue*' to type 'queue_handle' {aka 'queue*'} casts away qualifiers
17 | return reinterpret_cast<const queue_handle>(&d);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I'm converting to a const pointer though (which gcc seems to misunderstand somehow). I do not need to change the pointer after returning it. How do I accomplish this conversion without errors?