Why warning on static local variables ... ?

Munagala V. S. Ramanath ram@netcom.com
Mon Apr 20 15:27:00 GMT 1998


Alexandre Oliva <oliva@dcc.unicamp.br> writes:

> Munagala V S Ramanath <ram@netcom.com> writes:
> 
> > tmp1.C: In function `struct A<int,8> Mul<A<int,8>>(const struct A<int,8> &, const struct A<int,8> &)':
> > tmp1.C:51: warning: sorry: semantics of inline function static data `int const n' are wrong (you'll wind up with multiple copies)
> 
> AFAIK, this occurs only on some targets that do not support ELF weak
> symbols, or something like that.  It does not occur on Solaris 2.[56], 
> for instance, but it does occur in SunOS 4.1.3.  You can only notice
> this problem if the inline function is instantiated in two different
> compilation units.

    Ok, I'm on SunOS 4.1.2, so I guess I'll just have to live with the
    problem. I'm no expert on load module formats but I had assumed
    that the linker that comes with egcs had the necessary smarts to
    eliminate duplicate symbols of this sort.
    
    Does this also mean I have to use -fno-implicit-templates and
    manually instantiate templates (or use -frepo) to avoid duplicate
    copies of all templates (as I was doing with gcc-2.7.2) on SunOS ?

> > PROBLEM 2: As can be seen from the above program output, it seems to
> >    be invoking the wrong constructor [A(int) instead of A(&A)] for the
> >    declaration like: B f3( Mul<B>( f1, f1 ) );
> >    Is there some default rule that I'm overlooking or is it a bug ?
> 
> The compiler is allowed to optimize away such copy-constructor call.
> Instead of copy-constructing the object returned by Mul<B> into f3 and
> f4, egcs arranges that Mul<B> constructs f3 and f4 directly, in-place.

    This explains why A(&A) is _not_ called but does not explain why
    A(int) _is_ called instead in this case; as I see it, in the program
    output:

A(int): val = 2560
A(int): val = 2304
Mul: &n = 0x22a0, n = 8
A(int): val = 6553600
Mul: &n = 0x22a0, n = 8
A(int): val = 5308416
f1 = 10, f3 = 25600, f2 = 9, f4 = 20736

    The first two calls of A(int) are for f1 and f2; then Mul is called
    for f3 but then we have _another_ call to A(int) followed by yet
    another call to A(int) after Mul is called for f4 which is what I
    still don't understand.

    Thanks for all the explanations.

    Ram



More information about the Gcc mailing list