Topic: const linkage


Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Fri, 13 Jan 1995 02:17:11 GMT
Raw View
leblanck@austin.ibm.com (Karalee LeBlanc) writes:

>A friend and I are discussing const linkage, and we can't figure out the
>answer from the September 20 1994 draft of the Working Paper (it's probably
>there, we just can't find it).

See WD 3.4/3 and 7.1.1/6.

>My friend says that
>
>  const int myzero = 0;
>
>should have static linkage, and that the value of the symbolic constant
>will replace the occurrance of it's name during compilation.  So this
>line could be put in a header and compiled in many different libraries
>without any duplicate definitions occurring of myzero (which is what
>happens on his compiler).

Your friend is correct.

>I say it does not have static linkage

That's not correct.

>that you have, in essence, just
>declared 'myzero' readonly.  When I put a line like this in my headers,
>I get duplicate definitions of myzero in the different libraries.

Either you are using a C compiler rather than a C++ compiler,
or your C++ compiler is broken.

>I believe that if you wanted static linkage, you should have declared it:
>
>  int const myzero = 0;

No, the order of `int' and `const' doesn't make any difference.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au
all [L] (programming_language(L), L \= "Mercury") => better("Mercury", L) ;-)




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sun, 15 Jan 1995 21:36:30 GMT
Raw View
>> My friend says that
>
>>   const int myzero = 0;
>
>suspect that you are confusing C++ with C here.  In C++ myzero has the
>semantics of a constant (e.g., one may use it for array dimension
>declarations) and has static linkage.  In C myzero has the semantics of
>a variable except that one may not change it and has static linkage only if
>it is declared static.

 In C++, "myzero" is _both_ a constant and a variable. It is,
after all, addressable:

 int const *P0 = &myzero; // valid

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189