Topic: const and typedefs, inconsistent?


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/12/01
Raw View
In article qgu@shadow2.qnetix.ca, KevinM@lamsys.com (Kevin McIsaac) writes:
>A look at the April draft working papers at:
>http://www.cygnus.com/misc/wp/draft/dcl.html in section 7.1.2, shows the
>following examples:
>
>  typedef char* Pc;
>  void f(const Pc);       // void f(char* const)  (not const char*)
>  void g(const int Pc);   // void g(const int)
>
>May questions are: why the f(const Pc) resolves to char* const?

Because a typedef is not a macro; it gives a name to a type.

The prototype
 void f(const T); // T is the name of a type
says that the parameter of f has type T, and is const. The prototype
 void f(T const);
means exactly the same thing. It doesn't matter whether T is the name
of a pointer type or not.

When you write an explicit * in a declaration, it is part of the
declarator. So
 void g(const char*);
means that the parameter is a pointer, and it points to a const char.
Notice that the declaration
 void f(const T);
does not contain a *, as it would if you used a macro instead of a typedef.
---
Steve Clamage, stephen.clamage@eng.sun.com



---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: KevinM@lamsys.com (Kevin McIsaac)
Date: 1995/12/01
Raw View
A look at the April draft working papers at:
http://www.cygnus.com/misc/wp/draft/dcl.html in section 7.1.2, shows the
following examples:

  typedef char* Pc;
  void f(const Pc);       // void f(char* const)  (not const char*)
  void g(const int Pc);   // void g(const int)

May questions are: why the f(const Pc) resolves to char* const? Is the
associativity changed? And is it possible to format f(), using Pc, such that
it resolves to const char*? TIA.

--
// kev@lamsys.com
// What the heck did you think it was made with?


[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]