TITLE: typedefs and const (Newsgroups: comp.std.c++, 9 Jan 99) GREIF: "Jeff Greif" >> It appears that at least one compiler treats the type const PString, >> defined by >> >> typedef char* PString; >> >> as char const *, hence differently from the type CPString, defined as >> typedef const char* CPString; >> >> Is this correct according to the standard (I could not find relevant >> chapter and verse)? CLAMAGE: Steve Clamage >It's required by the standard. > >If T is the name of a type, > const T p; >means the same as > T const p; > >If you write > typedef char* PString; > const PString p; > PString const q; >then p and q have the same type. That is, the const does not >"penetrate" the typedef. > >Writing > const char* p; >is entirely different. The * attaches to the p, so p points to a >const char. > >There is no special rule in the standard to cover this situation. >It just falls out of the declaration grammar, and is the same in C >and C++. ------------------------------------------------------------------- (Newsgroups: comp.std.c++, 12 Jan 99) BIGGAR: Jonathan Biggar >>> >>> Note that a similar situation: >>> >>> typdef char Array[5]; >>> >>> const Array a; >>> >>> does not quite do the same thing according to the standard. This is >>> equivalent to: >>> >>> const char a[5]; CLAMAGE: stephen.clamage@sun.com (Steve Clamage) >> If you get that result, your compiler is in error. There is no such >> special case for arrays. ... >> >> The type of Array above is "array of 5 chars", and the "const" in your >> example applies (unnecessarily) to the "a", not to the array elements. CLAMAGE: Steve Clamage > As several people pointed out to me, I got that one wrong. > Jonathan was correct. > > I apologize for any confusion I may have caused. > > The special case is for "const" applied to array types or objects, > including typedefs. The "const" always modifies the array element > type, and does not modify the top-level array object. In the case > of arrays of arrays, the const also modifies the subarray objects > in addition to the ultimate array elements. (Weird, huh?)