Connecting Tech Pros Worldwide Forums | Help | Site Map

"const int" as function parameter type

qingning
Guest
 
Posts: n/a
#1: Oct 18 '06
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

Victor Bazarov
Guest
 
Posts: n/a
#2: Oct 18 '06

re: "const int" as function parameter type


qingning wrote:
Quote:
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.
Quote:
>
--cut--
$ cat constf2.cc
void f(const int);
>
void f(int)
{
}
Move this function definition below 'main', what do you get?
Quote:
>
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


qingning
Guest
 
Posts: n/a
#3: Oct 18 '06

re: "const int" as function parameter type




On Oct 18, 2:27 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Quote:
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
Quote:
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

Closed Thread