Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

typedef masking const ?

Status
Not open for further replies.

Salem

Programmer
Apr 29, 2003
2,455
0
0
GB
Here is a small section of code which has various typedefs for the same structure
Code:
#include <stdio.h>

struct node {
  struct node *next;
  char         fname[20];
  char         lname[20];
  int          age;
};

typedef struct node node_t;   /* typedef a struct */
typedef node_t *pnode_t;      /* typedef a non-const pointer to struct */
typedef const node_t *cnode_t;/* typedef a const pointer to struct */

/* vc++.net (and VC6 ) with W1 to W4 warning levels reports */
/* warning C4090: 'initializing' : different 'const' qualifiers */
/* gcc reports the following */
/*    tested with gcc (GCC) 3.3.1 (cygming special) */
/*    tested with gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7) */
/* warning: initialization discards qualifiers from pointer target type */
void foo ( const void *p ) {
  const pnode_t q = p;      /* (see NOTE) const applied to a non-const 'pointer' typedef */
  cnode_t       c = p;      /* 'pointer' typedef contains const */
  const node_t *r = p;      /* const explicitly stated for 'struct' typedef */
  const struct node *s = p; /* const explicitly stated for struct */

  printf( &quot;%s %s %s %s\n&quot;, q->fname, r->fname, s->fname, c->fname );
}

int main(void) {
  return 0;
}
The question is, why isn't the const on the first declaration having the desired effect, when all the other const declarations are OK.

It seems like the const cannot get 'inside' the typedef of pnode_t, to make the whole thing a const, whereas making the const part of the typedef itself (as in cnode_t) does the right thing.

--
 
>>typedef const node_t *cnode_t;/* typedef a const pointer to struct */

Your comment is wrong :
It's a pointer to a const object (You can modify the pointer, but not the object)

Except for that I can't really help you - triggy thing !

/JOlesen
 
Code:
int* a;             // non-const pointer to non-const
const int* b;       // non-const pointer to const
int* const c;       // const pointer to non-const
const int* const d; // const pointer to const

I think the typedef thing is sorta disrupting the &quot;precedence&quot; in your declarations. That is, declaring a typedef const is like
Code:
const (node_t*)
, and applies to the whole type and makes the pointer const instead of the pointee.

To get around this, you could use some fancy template metaprogramming:

Code:
  template< typename T >
struct ApplyConstToPointee
{
    typedef T Type;
};

  template< typename T >
struct ApplyConstToPointee<T*>
{
    typedef const T* Type;
};

  template< typename T >
struct ApplyConstToPointee<T* const>
{
    typedef const T* const Type;
};

...

ApplyConstToPointee<pnode_t>::Type q = p;

 
Whoops. I didn't realize I was in C. That last part is C++, so ignore it if it doesn't apply to what you're doing. Sorry 'bout that.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top