Topic: int vs const int function parameter


Author: David R Tribble <david@tribble.com>
Date: 1999/12/17
Raw View
I ran into a problem recently where a ctor was declared as taking a
'const int' parameter, but defined in the library code as taking an
'int' parameter:

    // foo.h -----------------------------

    class Foo
    {
    public:
        Foo();
        Foo(const int);   // [decl]
        ...
    };

    // foo.cpp ---------------------------

    #include "foo.h"

    Foo::Foo(int)         // [defn]
    {
        ...
    }

(I don't have the actual library source (foo.cpp), so I'm guessing
what the member function definition looks like based on the linker
error message I'm getting.)  The linker complains that it can't find
symbol 'Foo::Foo(const int)' in the library.

According to 8.3.5p3, these two prototypes result in the same
function signature:

    Foo::Foo(int)
    Foo::Foo(const int)

I.e., the 'const' is ignored for the purposes of deducing the type
of the function.

It therefore appears that my compiler/linker (HP-UX 11.0 C++ A.12.10)
has a bug in this regard.  If I'm not mistaken, the previous version
of HP's C++ compiler (shipped with HP-UX 10.20) did not have this
problem.  (I assume that a previous version was used to compile
the code in the first place.)

-- David R. Tribble, david@tribble.com, http://david.tribble.com --


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: mauricefxxx@ix.netcom.com (Maurice Fox)
Date: 1999/12/18
Raw View
On Fri, 17 Dec 1999 21:08:30, David R Tribble <david@tribble.com>
wrote:

>
> I ran into a problem recently where a ctor was declared as taking a
> 'const int' parameter, but defined in the library code as taking an
> 'int' parameter:
>
>     // foo.h -----------------------------
>
>     class Foo
>     {
>     public:
>         Foo();
>         Foo(const int);   // [decl]
>         ...
>     };
>
>     // foo.cpp ---------------------------
>
>     #include "foo.h"
>
>     Foo::Foo(int)         // [defn]
>     {
>         ...
>     }
>
> (I don't have the actual library source (foo.cpp), so I'm guessing
> what the member function definition looks like based on the linker
> error message I'm getting.)  The linker complains that it can't find
> symbol 'Foo::Foo(const int)' in the library.
>
> According to 8.3.5p3, these two prototypes result in the same
> function signature:
>
>     Foo::Foo(int)
>     Foo::Foo(const int)
>
> I.e., the 'const' is ignored for the purposes of deducing the type
> of the function.
>
> It therefore appears that my compiler/linker (HP-UX 11.0 C++ A.12.10)
> has a bug in this regard.  If I'm not mistaken, the previous version
> of HP's C++ compiler (shipped with HP-UX 10.20) did not have this
> problem.  (I assume that a previous version was used to compile
> the code in the first place.)


It shouldn't make any difference when the parameter is an int, because
it's pass by value.  The called function gets a copy of the parameter,
and can't do anything to the original value.

Maurice



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: wmm@fastdial.net
Date: 1999/12/18
Raw View
In article <385AA510.D39EF245@tribble.com>,
  David R Tribble <david@tribble.com> wrote:
>
> I ran into a problem recently where a ctor was declared as taking a
> 'const int' parameter, but defined in the library code as taking an
> 'int' parameter:
>
>     // foo.h -----------------------------
>
>     class Foo
>     {
>     public:
>         Foo();
>         Foo(const int);   // [decl]
>         ...
>     };
>
>     // foo.cpp ---------------------------
>
>     #include "foo.h"
>
>     Foo::Foo(int)         // [defn]
>     {
>         ...
>     }
>
> (I don't have the actual library source (foo.cpp), so I'm guessing
> what the member function definition looks like based on the linker
> error message I'm getting.)  The linker complains that it can't find
> symbol 'Foo::Foo(const int)' in the library.
>
> According to 8.3.5p3, these two prototypes result in the same
> function signature:
>
>     Foo::Foo(int)
>     Foo::Foo(const int)
>
> I.e., the 'const' is ignored for the purposes of deducing the type
> of the function.
>
> It therefore appears that my compiler/linker (HP-UX 11.0 C++ A.12.10)
> has a bug in this regard.  If I'm not mistaken, the previous version
> of HP's C++ compiler (shipped with HP-UX 10.20) did not have this
> problem.  (I assume that a previous version was used to compile
> the code in the first place.)

I can't speak for the bugginess or lack thereof of your compiler
(you should try a small experiment using from-scratch code to
narrow down the possibilities), but your understanding is correct:
"void foo(int);" and "void foo(const int);" both declare the same
function.
--
William M. Miller, wmm@fastdial.net
OnDisplay, Inc. (www.ondisplay.com)


Sent via Deja.com http://www.deja.com/
Before you buy.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]