const vs. typedef

  • Follow


I found the following behavior (with g++ and VS2008) with code like
this:

//////////////////////////////
typedef foo bar;
typedef const foo cbar;

void TestFunc( const bar m)
{}
void TestFunc2( foo r)
{}
void TestFunc3( cbar m)
{}

void MyTest()
{
   const foo cm;
   foo       m;

   TestFunc(m);   //22
   TestFunc2(m);  //23
   TestFunc3(m);  //24

   TestFunc(cm);   //26
   TestFunc2(cm); //27
   TestFunc3(cm); //28
}
//////////////////////////////

with 'foo' defined as (e.g.) 'char*' I see the compilation fail saying
(CodeBlocks build log):
----------------------
main.cpp:26: error: invalid conversion from `const char*' to `char*'
main.cpp:26: error:   initializing argument 1 of `void TestFunc
(char*)'
main.cpp:27: error: invalid conversion from `const char*' to `char*'
main.cpp:27: error:   initializing argument 1 of `void TestFunc2
(char*)'
----------------------

That already puzzels me, but with 'foo' defined as 'char**' it gets
worse:

----------------------
main.cpp:24: error: invalid conversion from `char**' to `const char**'
main.cpp:24: error:   initializing argument 1 of `void TestFunc3(const
char**)'
main.cpp:26: error: invalid conversion from `const char**' to `char**'
main.cpp:26: error:   initializing argument 1 of `void TestFunc
(char**)'
main.cpp:27: error: invalid conversion from `const char**' to `char**'
main.cpp:27: error:   initializing argument 1 of `void TestFunc2
(char**)'
----------------------

Can anyone explain, why this happens? Why does the const prepended to
the typedef seem to be ignored? And why does the error occur in line
24 for the 'char**' case?

Any help appreciated! Thanks!

Juergen

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply jaybus56 3/30/2009 5:36:30 AM

jaybus56 <busch.juergen@gmx.net> wrote in news:63b3c795-21d9-45f8-8459-
3081a299c996@37g2000yqp.googlegroups.com:

> I found the following behavior (with g++ and VS2008) with code like
> this:
> 
> //////////////////////////////
> typedef foo bar;
> typedef const foo cbar;
> 
> void TestFunc( const bar m)
> {}
> void TestFunc2( foo r)
> {}
> void TestFunc3( cbar m)
> {}

Be aware that making parameters const has no effect at the call site. 
That is, when calling a function there is no difference between

void f(int x);
void f(const int x);


> void MyTest()
> {
>    const foo cm;
>    foo       m;

You say below that foo is 'char *'. So I assume that cm is a constant 
pointer to character. However...

> with 'foo' defined as (e.g.) 'char*' I see the compilation fail saying
> (CodeBlocks build log):
> ----------------------
> main.cpp:26: error: invalid conversion from `const char*' to `char*'
> main.cpp:26: error:   initializing argument 1 of `void TestFunc


This message makes it sound like cm is a pointer to constant character. 
Keep in mind that there is a difference between

const char *cm;

and 

char *const cm;

In the second case the pointer is constant. In the first case it is the 
item pointed at that is constant. Now consider...

typedef char *foo;  // 'foo' is an alias for char *.

const foo cm;    // The pointer is constant.
char *const cm;  // This is the same thing.
const char *cm;  // This is something different.

Peter

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Peter 3/30/2009 1:09:05 PM


On 30 mar, 13:36, jaybus56 <busch.juer...@gmx.net> wrote:
> [const and pointer issues]
>
> Can anyone explain, why this happens? Why does the const prepended to
> the typedef seem to be ignored? And why does the error occur in line
> 24 for the 'char**' case?

typedef int* pint;
const pint is actually not const int*, but int* const.

It's the pointer that is constant, not the pointed value.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Mathias 3/30/2009 1:09:19 PM

On Mar 30, 1:36 pm, jaybus56 <busch.juer...@gmx.net> wrote:
> I found the following behavior (with g++ and VS2008) with code like
> this:
>
> //////////////////////////////
> typedef foo bar;
> typedef const foo cbar;
>
> void TestFunc( const bar m)
> {}
> void TestFunc2( foo r)
> {}
> void TestFunc3( cbar m)
> {}
>
> void MyTest()
> {
>    const foo cm;
>    foo       m;
>
>    TestFunc(m);   //22
>    TestFunc2(m);  //23
>    TestFunc3(m);  //24
>
>    TestFunc(cm);   //26
>    TestFunc2(cm); //27
>    TestFunc3(cm); //28}
>
> //////////////////////////////
>
> with 'foo' defined as (e.g.) 'char*' I see the compilation fail saying
> (CodeBlocks build log):
> ----------------------
> main.cpp:26: error: invalid conversion from `const char*' to `char*'
> main.cpp:26: error:   initializing argument 1 of `void TestFunc
> (char*)'
> main.cpp:27: error: invalid conversion from `const char*' to `char*'
> main.cpp:27: error:   initializing argument 1 of `void TestFunc2
> (char*)'
> ----------------------
>
> That already puzzels me, but with 'foo' defined as 'char**' it gets
> worse:
>
> ----------------------
> main.cpp:24: error: invalid conversion from `char**' to `const char**'
> main.cpp:24: error:   initializing argument 1 of `void TestFunc3(const
> char**)'
> main.cpp:26: error: invalid conversion from `const char**' to `char**'
> main.cpp:26: error:   initializing argument 1 of `void TestFunc
> (char**)'
> main.cpp:27: error: invalid conversion from `const char**' to `char**'
> main.cpp:27: error:   initializing argument 1 of `void TestFunc2
> (char**)'
> ----------------------
>
> Can anyone explain, why this happens? Why does the const prepended to
> the typedef seem to be ignored? And why does the error occur in line
> 24 for the 'char**' case?
>
> Any help appreciated! Thanks!
>
> Juergen
>

Okay, together with my colleagues I already found the solution (we
think). :)

////////////////////////////
const foo *cpf;   // typeof(*cpf) == "const foo"

typedef foo * pfoo;
const pfoo pfc;   // typeof(pfc) == "const pfoo", typeof(*pfc) ==
"foo"

// so: typeof(*cpf) != typeof(*pfc)
////////////////////////////

When we finally saw this, it was evident... ;-)


For the other case:

////////////////////////////
const foo** cfpp;
foo** fpp;

cfpp = fpp; // will fail

void MyFoo( const foo** cfpp)
{}

void MyBar()
{
foo** fpp;
MyFoo(fpp); // will fail
}
////////////////////////////

we still don't have a good explanation. Especially because:

////////////////////////////
const foo* const* cfpcp;
cfpcp = fpp; // will work

void MyFoo2( const foo* const* cfpp)
{}

void MyBar2()
{
foo** fpp;
MyFoo2(fpp); // will work
}
////////////////////////////

Any ideas here?


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply jaybus56 3/30/2009 1:11:05 PM

jaybus56 wrote:
> typedef foo bar;
> typedef const foo cbar;
[...]
> with 'foo' defined as (e.g.) 'char*'

What do you mean "defined"? Using a macro? The point is that const when
applied to pointers types can be put in three different positions:

  const T*
  T const*
  T* const

The first two are equivalent. However, the third one is a constant pointer,
not a pointer to a constant, see also the FAQ. However, if you do

  typedef T* pT;

....and then use a const on the resulting alias, you will always get a
constant pointer, regardless of where you place the const!

In other words, these two snippets are not equivalent:

  // 1
  typedef const foo cbar;
  // 2
  typedef foo bar;
  typedef const bar cbar;

BTW: you will stumble across this stanza in the FAQ, too, but let me mention
it once more here: Macros are evil.

cheers

Uli


-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Ulrich 3/30/2009 1:11:21 PM

jaybus56 wrote:
> I found the following behavior (with g++ and VS2008) with code like
> this:
> 
> //////////////////////////////
> typedef foo bar;
> typedef const foo cbar;
> 
> (...)
> with 'foo' defined as (e.g.) 'char*' I see the compilation fail saying
> (CodeBlocks build log):
> ----------------------
> main.cpp:26: error: invalid conversion from `const char*' to `char*'
> (...)
> That already puzzels me, but with 'foo' defined as 'char**' it gets
> worse:
> 
> ----------------------
> main.cpp:24: error: invalid conversion from `char**' to `const char**'
> (...)
> Can anyone explain, why this happens? Why does the const prepended to
> the typedef seem to be ignored? And why does the error occur in line
> 24 for the 'char**' case?
> 

You cannot "chain" the correct cinstness to pointers when using typedefs:

typedef  char*  pointer_to_char;
typedef  const char*  pointer_to_const_char;
// However(!):
typedef  const pointer_to_char  const_pointer_to_char;
// is equivalent:
typedef  char* const  const_pointer_to_char_2;

That is, when you have a typedef that is a pointer and you apply const 
to this type alias, you will not get a pointer-to-const but instead you 
will get a const pointer.

br,
Martin

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Martin 3/30/2009 1:11:42 PM

On 30 Mrz., 13:36, jaybus56 <busch.juer...@gmx.net> wrote:
> I found the following behavior (with g++ and VS2008) with
> code like this:
>
> //////////////////////////////
> typedef foo bar;
> typedef const foo cbar;

What's foo?

> void TestFunc( const bar m);
> void TestFunc2( foo r);
> void TestFunc3( cbar m);
>
> void MyTest()
> {
>    const foo cm;  //19
>    foo       m;   //20
>
>    TestFunc(m);   //22
>    TestFunc2(m);  //23
>    TestFunc3(m);  //24
>
>    TestFunc(cm);  //26
>    TestFunc2(cm); //27
>    TestFunc3(cm); //28
> }
>
> //////////////////////////////
>
> with 'foo' defined as (e.g.) 'char*'

  typedef char* foo;
  typedef foo bar;
  typedef const foo cbar;

Ok, so

  foo       = char*
  bar       = char*
  const foo = char* const
  const bar = char* const
  cbar      = char* const

  TestFunc  accepts 'char*'
  TestFunc2 accepts 'char*'
  TestFunc3 accepts 'char*'

  cm is of type 'char* const'
  m  is of type 'char*'

Since cm is const and not initialized I get a compilation error for
line 19.  If I add "=0" in line 19 and line 20 it compiles.

> I see the compilation fail saying
> (CodeBlocks build log):
> ----------------------
> main.cpp:26: error: invalid conversion from `const char*'
> to `char*'
> main.cpp:26: error:   initializing argument 1 of `void
> TestFunc (char*)'

That's weird.  Are you sure that these are the errors for
foo='char*'?  There's no 'const char*' involved here -- only const
pointers to non-const characters.

> main.cpp:27: error: invalid conversion from `const char*'
> to `char*'
> main.cpp:27: error:   initializing argument 1 of `void
> TestFunc2 (char*)'

Same here.  I can't reproduce this.  It looks like you didn't post the
same code you tried to compile.


Cheers!
SG

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply SG 3/30/2009 1:13:08 PM

On Mar 30, 7:36 pm, jaybus56 <busch.juer...@gmx.net> wrote:
> I found the following behavior (with g++ and VS2008) with code like
> this:
>
> //////////////////////////////
> typedef foo bar;
> typedef const foo cbar;
>
> void TestFunc( const bar m)
> {}
> void TestFunc2( foo r)
> {}
> void TestFunc3( cbar m)
> {}
>
> void MyTest()
> {
>    const foo cm;
>    foo       m;
>
>    TestFunc(m);   //22
>    TestFunc2(m);  //23
>    TestFunc3(m);  //24
>
>    TestFunc(cm);   //26
>    TestFunc2(cm); //27
>    TestFunc3(cm); //28}
>
> //////////////////////////////
>
> with 'foo' defined as (e.g.) 'char*' I see the compilation fail saying
> (CodeBlocks build log):
> ----------------------
> main.cpp:26: error: invalid conversion from `const char*' to `char*'
> main.cpp:26: error:   initializing argument 1 of `void TestFunc
> (char*)'
> main.cpp:27: error: invalid conversion from `const char*' to `char*'
> main.cpp:27: error:   initializing argument 1 of `void TestFunc2
> (char*)'
> ----------------------
>
> That already puzzels me, but with 'foo' defined as 'char**' it gets
> worse:
>
> ----------------------
> main.cpp:24: error: invalid conversion from `char**' to `const char**'
> main.cpp:24: error:   initializing argument 1 of `void TestFunc3(const
> char**)'
> main.cpp:26: error: invalid conversion from `const char**' to `char**'
> main.cpp:26: error:   initializing argument 1 of `void TestFunc
> (char**)'
> main.cpp:27: error: invalid conversion from `const char**' to `char**'
> main.cpp:27: error:   initializing argument 1 of `void TestFunc2
> (char**)'
> ----------------------
>
> Can anyone explain, why this happens? Why does the const prepended to
> the typedef seem to be ignored? And why does the error occur in line
> 24 for the 'char**' case?
>
> Any help appreciated! Thanks!
>
> Juergen
>

I did not try VC2008 since I have not installed it.
But I can compile it under GCC(4.3.2) without any error if foo is
char*.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply chaozh 3/30/2009 1:13:43 PM

On Mar 30, 7:36 pm, jaybus56 <busch.juer...@gmx.net> wrote:
> I found the following behavior (with g++ and VS2008) with code like
> this:
>
> //////////////////////////////
> typedef foo bar;
> typedef const foo cbar;
>
> void TestFunc( const bar m)
> {}
> void TestFunc2( foo r)
> {}
> void TestFunc3( cbar m)
> {}
>
> void MyTest()
> {
>    const foo cm;
>    foo       m;
>
>    TestFunc(m);   //22
>    TestFunc2(m);  //23
>    TestFunc3(m);  //24
>
>    TestFunc(cm);   //26
>    TestFunc2(cm); //27
>    TestFunc3(cm); //28}
>
> //////////////////////////////
>
> with 'foo' defined as (e.g.) 'char*' I see the compilation fail saying
> (CodeBlocks build log):
> ----------------------
> main.cpp:26: error: invalid conversion from `const char*' to `char*'
> main.cpp:26: error:   initializing argument 1 of `void TestFunc
> (char*)'
> main.cpp:27: error: invalid conversion from `const char*' to `char*'
> main.cpp:27: error:   initializing argument 1 of `void TestFunc2
> (char*)'
> ----------------------
>
> That already puzzels me, but with 'foo' defined as 'char**' it gets
> worse:
>
> ----------------------
> main.cpp:24: error: invalid conversion from `char**' to `const char**'
> main.cpp:24: error:   initializing argument 1 of `void TestFunc3(const
> char**)'
> main.cpp:26: error: invalid conversion from `const char**' to `char**'
> main.cpp:26: error:   initializing argument 1 of `void TestFunc
> (char**)'
> main.cpp:27: error: invalid conversion from `const char**' to `char**'
> main.cpp:27: error:   initializing argument 1 of `void TestFunc2
> (char**)'
> ----------------------
>
> Can anyone explain, why this happens? Why does the const prepended to
> the typedef seem to be ignored? And why does the error occur in line
> 24 for the 'char**' case?
>
> Any help appreciated! Thanks!
>
> Juergen
>

You can not pass a char** into function with const char** parameter,
It is different with char* and const char*. You can refer to 《Expert C
Programming》Chapter2, There is a detail explanation.
And for const bar cm, the const is effected on cm not bar(my
understanding). It means the typeid(const bar) is equal typeid(char**
const) not typeid(const char**) or typeid(const char* const*).
If foo is char*, I can compile it under GCC(4.3.2) successfully.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply chaozh 3/30/2009 1:18:14 PM

> Can anyone explain, why this happens? Why does the const prepended to
> the typedef seem to be ignored? And why does the error occur in line
> 24 for the 'char**' case?
>

When you declare a typedef for a pointer, adding const to that typedef
actually makes the pointer const, not the object... so by your example

typedef foo bar;
typedef const foo cbar;

void TestFunc( const bar m)
{}

In your case if bar = char* you were probably hoping for the above to
be read as:
void TestFunc(const char* m) {}

but acutally its read as

void TestFunc (char * const  m) {}

You probably already know the difference... but in case we need a
refresher:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.4

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Jeremy 3/30/2009 1:18:53 PM

On Mar 30, 4:36 am, jaybus56 <busch.juer...@gmx.net> wrote:
> I found the following behavior (with g++ and VS2008) with code like
> this:
>
> //////////////////////////////
> typedef foo bar;
> typedef const foo cbar;
>
> void TestFunc( const bar m)
> {}
> void TestFunc2( foo r)
> {}
> void TestFunc3( cbar m)
> {}
>
> void MyTest()
> {
>    const foo cm;
>    foo       m;
>
>    TestFunc(m);   //22
>    TestFunc2(m);  //23
>    TestFunc3(m);  //24
>
>    TestFunc(cm);   //26
>    TestFunc2(cm); //27
>    TestFunc3(cm); //28}
>
> //////////////////////////////
>
> with 'foo' defined as (e.g.) 'char*'

First of all, let me clarify - by "defined" here you mean literally
this, right?

  #define foo char*

(or the equivalent compiler switch) - because that would explain all
of your problems.

> I see the compilation fail saying
> (CodeBlocks build log):
> ----------------------
> main.cpp:26: error: invalid conversion from `const char*' to `char*'
> main.cpp:26: error:   initializing argument 1 of `void TestFunc
> (char*)'
> main.cpp:27: error: invalid conversion from `const char*' to `char*'
> main.cpp:27: error:   initializing argument 1 of `void TestFunc2
> (char*)'
> ----------------------
>
> That already puzzels me, but with 'foo' defined as 'char**' it gets
> worse:
>
> ----------------------
> main.cpp:24: error: invalid conversion from `char**' to `const char**'
> main.cpp:24: error:   initializing argument 1 of `void TestFunc3(const
> char**)'
> main.cpp:26: error: invalid conversion from `const char**' to `char**'
> main.cpp:26: error:   initializing argument 1 of `void TestFunc
> (char**)'
> main.cpp:27: error: invalid conversion from `const char**' to `char**'
> main.cpp:27: error:   initializing argument 1 of `void TestFunc2
> (char**)'
> ----------------------
>
> Can anyone explain, why this happens? Why does the const prepended to
> the typedef seem to be ignored? And why does the error occur in line
> 24 for the 'char**' case?

What happens is that you're confusing "const char*" with "char*
const". Consider your typedefs once expanded:

  typedef char* bar;
  typedef const char* cbar;

So bar is a pointer to a char, and cbar is a pointer to a const char.
Now consider your function declarations. In this case:

  void TestFunc(const bar m)

const applies to bar as a whole, not to the "char" part of it. So, if
we expand the typedef, we get:

  void TestFunc(char* const bar);

This "const" gets immediately discarded in the prototype (since it
serves no purpose), and you just get "char* bar". And then, at line
#26, you try to pass a "const char*" as the argument to that function
- which, of course, fails.

The same logic applies to the rest of your error cases.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Pavel 3/30/2009 1:31:21 PM

On Mar 30, 9:18 pm, chaozh <chao...@gmail.com> wrote:
> On Mar 30, 7:36 pm, jaybus56 <busch.juer...@gmx.net> wrote:
>
>
>
> > I found the following behavior (with g++ and VS2008) with code like
> > this:
>
> > //////////////////////////////
> > typedef foo bar;
> > typedef const foo cbar;
>
> > void TestFunc( const bar m)
> > {}
> > void TestFunc2( foo r)
> > {}
> > void TestFunc3( cbar m)
> > {}
>
> > void MyTest()
> > {
> >    const foo cm;
> >    foo       m;
>
> >    TestFunc(m);   //22
> >    TestFunc2(m);  //23
> >    TestFunc3(m);  //24
>
> >    TestFunc(cm);   //26
> >    TestFunc2(cm); //27
> >    TestFunc3(cm); //28}
>
> > //////////////////////////////
>
> > with 'foo' defined as (e.g.) 'char*' I see the compilation fail saying
> > (CodeBlocks build log):
> > ----------------------
> > main.cpp:26: error: invalid conversion from `const char*' to `char*'
> > main.cpp:26: error:   initializing argument 1 of `void TestFunc
> > (char*)'
> > main.cpp:27: error: invalid conversion from `const char*' to `char*'
> > main.cpp:27: error:   initializing argument 1 of `void TestFunc2
> > (char*)'
> > ----------------------
>
> > That already puzzels me, but with 'foo' defined as 'char**' it gets
> > worse:
>
> > ----------------------
> > main.cpp:24: error: invalid conversion from `char**' to `const char**'
> > main.cpp:24: error:   initializing argument 1 of `void TestFunc3(const
> > char**)'
> > main.cpp:26: error: invalid conversion from `const char**' to `char**'
> > main.cpp:26: error:   initializing argument 1 of `void TestFunc
> > (char**)'
> > main.cpp:27: error: invalid conversion from `const char**' to `char**'
> > main.cpp:27: error:   initializing argument 1 of `void TestFunc2
> > (char**)'
> > ----------------------
>
> > Can anyone explain, why this happens? Why does the const prepended to
> > the typedef seem to be ignored? And why does the error occur in line
> > 24 for the 'char**' case?
>
> > Any help appreciated! Thanks!
>
> > Juergen
>
> You can not pass a char** into function with const char** parameter,
> It is different with char* and const char*. You can refer to 《Expert C
> Programming》Chapter2, There is a detail explanation.
> And for const bar cm, the const is effected on cm not bar(my
> understanding). It means the typeid(const bar) is equal typeid(char**
> const) not typeid(const char**) or typeid(const char* const*).
> If foo is char*, I can compile it under GCC(4.3.2) successfully.

{ edits: quoted banner removed. don't quote extraneous material. tia. -mod }


Can you please refer to the page in chapter 2?

I found a lot about descrambling (which I believe I understand) but I
can't find an explanation, why passing a "foo **" to a function taking
"const foo**" could be bad when passing it to a function taking
"foo**" or "const foo* const*" is okay.


Say we have
  foo **obj; //(1)
  const foo **cobj; //(2)
  const foo * const *ccobj; //(3)

Then "*obj" is a "foo *", "*cobj" is a "const foo *" and  "**cobj" is
a "const foo *" too. The only difference between "cobj" and "ccobj" is
I am allowed to code
  *(cobj+1) = 0; //(4)
but not
  *(ccobj+1) = 0; //(5) fails

Because I'm also allowed to write
  *(obj+1) = 0; //(6)
what is so bad with (4) or so much "better" with (6)?


{ FAQ: this issue is discussed in current FAQ item 18.7, available at <url:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17>. -mod }

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply jaybus56 3/31/2009 9:29:11 PM

On 30 Mrz., 21:11, jaybus56 <busch.juer...@gmx.net> wrote:
> Okay, together with my colleagues I already found the solution (we
> think). :)
>
> const foo *cpf;   // typeof(*cpf) == "const foo"

Since you still havn't told us what "foo" is (!!!) there are two cases
to consider: Is it a macro or a typedef?

case 1:
     #define foo char*
     const foo *cpf;

case 2:
     typedef char* foo;
     const foo *cpf;

In the 1st case the type of cpf is char const**
In the 2nd case the type of cpf is char* const*

If you dereference theses pointers you get:
1. char const*
2. char* const

and const foo is
1. const char*
2. char* const

So, your assertion "typeof(*cpf) == const foo" holds in both cases.
Yet, the types involved in case 1 are not the same as the types in
case 2. (!!!)

Cheers!
SG


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply SG 4/1/2009 7:33:21 AM

12 Replies
71 Views

(page loaded in 0.12 seconds)

Similiar Articles:


















7/28/2012 10:47:35 AM


Reply: