On Thu, 3 Jan 2008 08:01:35 -0800 (PST), Keith Degrace
<de******@hotmail.comwrote in comp.lang.c++:
VC++ 2003 fails to compile the following code. I figured it may just
be a bug in VC, so I tried with Comeau Online and I get a similar
result. Now I am wondering if I've perhaps encountered an oddity in
the C++ standard.
typedef int *IntPtr;
void foo(const IntPtr)
{
}
void bar(const int &value)
{
foo(&value);
}
VC++ reports:
error C2664: 'foo' : cannot convert parameter 1 from 'const int *__w64
' to 'const IntPtr'
Conversion loses qualifiers
Comeau reports:
"ComeauTest.c", line 9: error: argument of type "const int *" is
incompatible with parameter of type "IntPtr"
If I remove IntPtr, and change foo() to take a int * explicitly, it
compiles fine. If I remove the const, it compiles fine.
Any ideas?
The wording I like to use for this is quite simple. cv qualifiers do
not "penetrate" a typedef.
Given your typedef:
typedef int *IntPtr;
You think that using "const IntPtr ip" is equivalent to just putting
the const keyword in front, like this:
const int *ip
....meaning ip is pointer to const int, but it is not. A qualifier
like const, volatile, and restrict, if that ever gets added to C++,
does not "penetrate" a typedef. Any qualifiers applied to a
declaration involving the typedef always apply to the object defined,
so:
const IntPtr ip;
....is equivalent to:
int * const ip;
....meaning ip is a const pointer to a non-const int.
Using your typedef, there is absolutely no way to define a non-const
pointer to a const int.
This is one of the reasons why it is almost always a bad idea to
typedef pointers to any type of objects. There are exceptions, but
they are very few.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html