"const int" as function parameter type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • qingning

    "const int" as function parameter type

    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

    #2
    Re: "const int" as function parameter type

    qingning wrote:
    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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • qingning

      #3
      Re: "const int" as function parameter type



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

      Comment

      Working...