"const int" as function parameter type

Q

qingning

Hi,

I've encountered code declaring functions with "const int" as parameter
type, but use "int" when defining the function. This worked fine with
g++, but failed at linking time with Sun CC. The following is an
example.

--cut--
$ cat constf.h
void f(const int);

$ cat constf.cc
#include "constf.h"
void f(int)
{
}

$ cat mconstf.cc
#include "constf.h"
int main()
{
f(0);
}

$ CC constf.cc mconstf.cc
constf.cc:
mconstf.cc:
Undefined first referenced
symbol in file
void f(const int) mconstf.o
ld: fatal: Symbol referencing errors. No output written to a.out

$ CC -V
CC: Sun C++ 5.8 2005/10/13
--cut--

Is this valid c++ code? If I put everything to a single source file, CC
compiles and links fine. I am wondering whether Sun CC linker is at
fault.

--cut--
$ cat constf2.cc
void f(const int);

void f(int)
{
}

int main()
{
f(0);
}

$ CC constf2.cc
$
--cut--

Thanks for help.

qingning
 
V

Victor Bazarov

qingning said:
I've encountered code declaring functions with "const int" as
parameter type, but use "int" when defining the function. This
worked fine with g++, but failed at linking time with Sun CC. The
following is an example.

--cut--
$ cat constf.h
void f(const int);

$ cat constf.cc
#include "constf.h"
void f(int)
{
}

$ cat mconstf.cc
#include "constf.h"
int main()
{
f(0);
}

$ CC constf.cc mconstf.cc
constf.cc:
mconstf.cc:
Undefined first referenced
symbol in file
void f(const int) mconstf.o
ld: fatal: Symbol referencing errors. No output written to a.out

$ CC -V
CC: Sun C++ 5.8 2005/10/13
--cut--

Is this valid c++ code? If I put everything to a single source file,
CC compiles and links fine. I am wondering whether Sun CC linker is
at fault.

Don't judge too soon. Move the function definition and possibly see
it fail again. Then contact Sun Microsystems and tell them to fix
their compiler/linker.
--cut--
$ cat constf2.cc
void f(const int);

void f(int)
{
}

Move this function definition below 'main', what do you get?
int main()
{
f(0);
}

$ CC constf2.cc
$
--cut--

Yes, it's valid code. Top-level 'cv-qualifiers' do not participate in
the function type, so 'f(int const)' *should be* the same as 'f(int)'.

V
 
Q

qingning

Don't judge too soon. Move the function definition and possibly see
it fail again. Then contact Sun Microsystems and tell them to fix
their compiler/linker.

I moved the body of f() after main(). It still compiles fine.

But you are right. It's the fault of CC compiler.

$ nm constf.o | c++filt
00000010 T void f(int)

$ nm mconstf.o | c++filt
U void f(const int)
00000000 A __fsr_init_value
00000010 T main
Yes, it's valid code. Top-level 'cv-qualifiers' do not participate in
the function type, so 'f(int const)' *should be* the same as 'f(int)'.

I agree that this is valid c++. Thanks.

Qingning
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top