This code doesn't compile:
struct s_t {
int a;
};
struct c_s_t {
const int a;
};
s_t s;
c_s_t *c_s = &s;
ibug@ubuntu:~ $ g++ -fsyntax-only t.cpp
t.cpp:10:15: error: cannot convert ‘s_t*’ to ‘c_s_t*’ in initialization
c_s_t *c_s = &s;
^
However this one compiles perfectly:
int a, *pa = &a, **ppa = &pa, ***pppa = &ppa;
const int * const * const * const cpcpcpa = pppa;
I understand that a pointer that is more CV-qualified at any level can point to a less CV-qualified object at any level, but why isn't it the same for structures?
The above problem statement is a MCVE of a more complex problem, where my friend was trying to convert pointers between t_s_t<T> and t_s_t<const T>, where t_s_t is a template struct type with one template parameter typename, and T is an arbitrary type.
The reason is that
s_tandc_s_tare different types.Even if you define
c_s_tas:Then:
It is still not going to work.