Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning for const in typedef

When I was compiling a C++ program using icc 11, it gave this warning:

warning #21: type qualifiers are meaningless in this declaration
typedef const direction_vector_ref_t direction_vector_cref_t;

It is saying const just meaningless. I am curious about this since if this typedef expands it will turn into const array<double,3>& and the const is definitely meaningful. Why it gave this warning?

like image 825
xis Avatar asked Dec 28 '22 14:12

xis


1 Answers

direction_vector_ref_t, I pressume, its a reference. References are const by design, so adding const to a reference is meaningless. What you probably want is to make a reference to a const object, which can't be done by a typedef. You will have to repeat the slightly modified typedef definition.

Just to clarify:

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

The last line is the same as the first one, not the second one. Typedefs are not token pasting, once you typedef T& as ref, then ref refers to the reference to T type. If you add const to it, then you get a const reference to T type, not a reference to a const T type.

like image 182
K-ballo Avatar answered Jan 14 '23 14:01

K-ballo