473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Flummoxed by a compiler error

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?
Jan 3 '08 #1
3 1443
Keith Degrace wrote:
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.
You mean, when you write

foo ( const int * )

instead of

foo ( const IntPtr )
Note: typedef and #define are different.

If I remove the const, it compiles fine.

Any ideas?
Distinguish

a) a constant pointer to a non-const int
b) a non-const pointer to a const int

Ask yourself what

const IntPtr

is when IntPtr is int*.
Best

Kai-Uwe Bux
Jan 3 '08 #2
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
Jan 4 '08 #3
In article <m6********************************@4ax.com>,
ja*******@spamcop.net says...

[ ... ]
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.
Just pay close attention to Jack's precise wording: functions are not
objects, and creating a typedef of a pointer to a function does not fall
within what he said.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class...
2
by: Mary | last post by:
Hello, I am having a problem with the cl compiler. I have written a C class (RegConnect.c) which uses Win32 API functions such as RegOpenKey, RegCloseKey etc. Initially when I was trying to...
0
by: rollasoc | last post by:
Hi, I seem to be getting a compiler error Internal Compiler Error (0xc0000005 at address 535DB439): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around...
1
by: Ayende Rahien | last post by:
reparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error (0xc0000005 at address 53168B12): likely culprit is 'BIND'. An internal...
3
by: Mark Rockman | last post by:
------ Build started: Project: USDAver2, Configuration: Debug .NET ------ Preparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error...
4
by: David Sworder | last post by:
Consider the following line of code (it's not important what it does): resp.DocItem=Relations.SelectDocItems_BySearchString(req.SearchPhrase); It turns out that this line is in error. The...
6
by: David Lack | last post by:
Hi, I recently installed a 60-day trial of .NET 2003 on my development system. I made tests with previous personal projects (which compiled ok with VC6) and some open source files, and keep...
1
by: DesCF | last post by:
Everything was working fine until suddenly this problem appeared from nowhere: Boot up the pc and go into SQL Server Management Studio. The database is present and the data can be accessed. Go...
27
by: Dave | last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU compilers. I then decided to try to use Sun's compiler. The Sun Studio 12 compiler reports the following code, which is in...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.