Warning for const in typedef

When I compiled the program in C ++ using icc 11, it gave the following warning:

warning No. 21: type classifiers do not make sense in this declaration
typedef const direction_vector_ref_t direction_vector_cref_t;

They say that const simply pointless. This is interesting to me, because if this typedef expands, it will turn into a const array<double,3>& , and const definitely significant. Why did he give this warning?

+4
source share
2 answers

Are you sure? Try:

 array<double, 3> a; direction_vector_cref_t b = a; b[0] = 1.0; 

The problem here is that when using typedef, it conceptually adds parentheses around the type, so you conceptually use const (array<double, 3>&) and not (const array<double, 3>)& , so you actually don't referent object is permanent. So your declaration is more like:

 typedef array<double, 3>& const direction_vector_cref_t; 

And in the above case, the constant for the variable (and not the type of referent) should be deferred to the end.

+4
source

direction_vector_ref_t, I will give his link. Links are const in design, so adding a const to a link is pointless. You probably want to reference a const object that cannot be executed with a typedef. You will have to repeat the slightly modified typedef definition.

Just clarify:

 typedef T& ref; typedef const T& cref; typedef const ref cref; 

The last line is the same as the first, not the second. Typedefs are not symbolic inserts, as soon as you type T & as ref, then ref refers to a reference to type T. If you add const to it, you will get a reference to const for type T, and not a reference to type const T.

+6
source

All Articles