Topic: Does/Will standard address binding in inline functions?


Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Wed, 25 May 1994 17:49:47 GMT
Raw View
In article <95L2tALOBh107h@rcp.co.uk> alan@rcp.co.uk (Alan Stokes) writes:
>In <Cpu81A.Bt1@ucc.su.OZ.AU> maxtal@physics.su.OZ.AU (John Max Skaller) writes:
>> Nothing is obvious. :-)
>
>> Do two pointers to members to the same inline
>>member in two translation units compare equal?
>> Do pointers to inline static members compare equal?
>The answer should be the same. It ought to be yes. C certainly says that
>two pointers to the same function compare equal. I don't see anything in the
>ARM & I'm not aware of anything in the draft standard that relaxes this rule
>for inline functions.

 Then you have not understood two things: C doesnt have
inline functions, so the C rule does not apply. And the ARM rule
is interpreted as saying inline members have internal linkage
by many and that REQUIRES the addresses be NOT EQUAL.

 I think the ARM rules are poor and should be changed,
in particular they are inconsistent for static inlines,
where external linkage is required but most implementors
cannot supply it.

>
>> May an inline member refer to a static variable?
>Definitely yes.

 If its permitted, no diagnostics of ODR violations will
exist. On the other hand it is not useful. So why allow it?
(I mean non-local static)


--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Mon, 23 May 1994 03:19:25 GMT
Raw View
chase@Think.COM (David Chase) writes:

>maxtal@physics.su.OZ.AU writes:
>|>  Nothing is obvious. :-)
>|>
>|>  Do two pointers to members to the same inline
>|> member in two translation units compare equal?
>|>
>|>  Do pointers to inline static members compare equal?
>|>
>|>  May an inline member refer to a static variable?
>
>No, this is still obvious.  In all cases, treat inline as
>a hint to the compiler, which means that the answer to all
>your questions is "whatever is true for a non-inline function".

That may be the obvious answer, but it's not C++.

The C++ answer is rather more gruesome and complex,
but slightly easier to implement.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Mon, 23 May 1994 12:27:54 GMT
Raw View
In article <9414313.5504@mulga.cs.mu.OZ.AU> fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:
>chase@Think.COM (David Chase) writes:
>
>>maxtal@physics.su.OZ.AU writes:
>>|>  Nothing is obvious. :-)
>>|>
>>|>  Do two pointers to members to the same inline
>>|> member in two translation units compare equal?
>>|>
>>|>  Do pointers to inline static members compare equal?
>>|>
>>|>  May an inline member refer to a static variable?
>>
>>No, this is still obvious.  In all cases, treat inline as
>>a hint to the compiler, which means that the answer to all
>>your questions is "whatever is true for a non-inline function".
>
>That may be the obvious answer, but it's not C++.
>
>The C++ answer is rather more gruesome and complex,
>but slightly easier to implement.

 Nah. The answer at the moment is that its IMPOSSIBLE
to implement because its not defined properly. In particular,
it is not clear if a static member with an inline definition
is allowed.

 There ARE examples in the ARM. But the ARM contradicts
itself by saying static member functions have external linkage,
and then saying inline methods have internal linkage.

 Which is it?

 It makes a difference, when you take the address.
If external linkage, the address is unique. If internal linkage,
the address MUST vary for each translation unit. That makes
inline different to a hint -- its a fundamental difference
in semantics.

The same question can be asked for non-static members:
do the addresses of the same non-static inline member of
a class have to compare equal or unequal?

 In my opinion, the BEST answer is: external linkage.
BEST for the programmer, that is. Not so easy for implementors
having to cope with stupid environments though.

 In any case -- its an open issue, a decision will
probably be made at Waterloo in July.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: chase@Think.COM (David Chase)
Date: 17 May 1994 22:14:39 GMT
Raw View
maxtal@physics.su.OZ.AU writes:
|>  Nothing is obvious. :-)
|>
|>  Do two pointers to members to the same inline
|> member in two translation units compare equal?
|>
|>  Do pointers to inline static members compare equal?
|>
|>  May an inline member refer to a static variable?

No, this is still obvious.  In all cases, treat inline as
a hint to the compiler, which means that the answer to all
your questions is "whatever is true for a non-inline function".

Whether any particular compiler can deliver actual inline
compilation is (as they say) a "quality of implementation"
issue.  The fallback (don't inline) is always available.
I know that I could implement this, if someone wanted
it badly enough to pay me to do it.  It isn't as easy as
falling off a log, but it should be easier than template
instantiation.  Similar problems have been solved in the
past for other languages.

David Chase, speaking for myself
Thinking Machines Corp.




Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Thu, 19 May 1994 09:33:01 GMT
Raw View
In article <95L2tALOBh107h@rcp.co.uk> alan@rcp.co.uk (Alan Stokes) writes:
>
>> May an inline member refer to a static variable?
>Definitely yes.

I might be misinterpreting, but I believe that the intended question was
`What will the following code do?'

 common.h:
 ----------------------------------------------------------------
 inline int last_value (int arg)
 {
     static int save;
     register int ret_value = save;

     save = arg;
     return ret_value;
 }
 ----------------------------------------------------------------

 one.c:
 ----------------------------------------------------------------
 void caller1 ()
 {
     last_value (99);
 }
 ----------------------------------------------------------------


 two.c:
 ----------------------------------------------------------------
 #include <stdio.h>
 #include "common.h"

 extern void caller1 ();

 int main ()
 {
     caller1 ();
     printf ("%d\n", last_value (77));
     return 0;
 }
 ----------------------------------------------------------------

Does this code print `99' or `0'?

The answer depends upon whether or not all inlined instances of the inline
function `last_value' share a single copy of the local static variable
named `save'.

Last time I checked, different C++ compilers yielded different results
for this kind of example.

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -




Author: kanze@us-es.sel.de (James Kanze)
Date: 19 May 1994 16:07:49 GMT
Raw View
In article <rfgCq1MJ2.B2K@netcom.com> rfg@netcom.com (Ronald F.
Guilmette) writes:

|> In article <95L2tALOBh107h@rcp.co.uk> alan@rcp.co.uk (Alan Stokes) writes:

|> >> May an inline member refer to a static variable?
|> >Definitely yes.

|> I might be misinterpreting, but I believe that the intended question was
|> `What will the following code do?'

|>  common.h:
|>  ----------------------------------------------------------------
|>  inline int last_value (int arg)
|>  {
|>      static int save;
|>      register int ret_value = save;

|>      save = arg;
|>      return ret_value;
|>  }
|>  ----------------------------------------------------------------

|>  one.c:
|>  ----------------------------------------------------------------
|>  void caller1 ()
|>  {
|>      last_value (99);
|>  }
|>  ----------------------------------------------------------------


|>  two.c:
|>  ----------------------------------------------------------------
|>  #include <stdio.h>
|>  #include "common.h"

|>  extern void caller1 ();

|>  int main ()
|>  {
|>      caller1 ();
|>      printf ("%d\n", last_value (77));
|>      return 0;
|>  }
|>  ----------------------------------------------------------------

|> Does this code print `99' or `0'?

According to the ARM, `0'.  Inline of a non-member function causes the
function to become static, e.g.: the two (identical) functions in
one.c and two.c have nothing to do with one another.

The question becomes more interesting if the inline function is a
member.  I believe that in this case, the results should be `99'.  But
I wouldn't get upset if they just banned local static data from inline
member functions.

And no, I don't like the fact that an identical function have
different semantics depending on whether it is a member or not.

|> The answer depends upon whether or not all inlined instances of the inline
|> function `last_value' share a single copy of the local static variable
|> named `save'.

|> Last time I checked, different C++ compilers yielded different results
|> for this kind of example.

Which means that no portably working code uses the feature, so we can
ban it?
--
James Kanze                       email: kanze@lts.sel.alcatel.de
GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                   -- Beratung in industrieller Datenverarbeitung




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Wed, 18 May 1994 17:15:45 GMT
Raw View
In article <2rbfkfINN7ih@early-bird.think.com> chase@Think.COM (David Chase) writes:
>maxtal@physics.su.OZ.AU writes:
>|>  Nothing is obvious. :-)
>|>
>|>  Do two pointers to members to the same inline
>|> member in two translation units compare equal?
>|>
>|>  Do pointers to inline static members compare equal?
>|>
>|>  May an inline member refer to a static variable?
>
>No, this is still obvious.  In all cases, treat inline as
>a hint to the compiler, which means that the answer to all
>your questions is "whatever is true for a non-inline function".

 Not obvious. The committee is divided on the issue.
The ARM is confused on it so its obviously not obvious. :-)

>Whether any particular compiler can deliver actual inline
>compilation is (as they say) a "quality of implementation"
>issue.  The fallback (don't inline) is always available.

 Thats NOT the point. The point is how the
compiler decides on the unique address of an inline function
with external linkage. And whether in fact inline
members of classes in particular have external linkage.

 If they dont, the same member of a class has different
addresses in two translation units. GAK!

 If they must, then the compiler has to decide WHERE
to instantiate the function -- and it cant do that. Only
the linker can do it. And Unix linkers cant do it, they're
not smart enough.

>I know that I could implement this, if someone wanted
>it badly enough to pay me to do it.  It isn't as easy as
>falling off a log, but it should be easier than template
>instantiation.  Similar problems have been solved in the
>past for other languages.

 I agree. But I alone do not determine the outcome. :-)
And there are alternatives. Some would and do argue for
internal linkage and claim the ARM and existing practice back them up.
Their claims cannot be easily dismissed. The alternative is also
viable.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: alan@rcp.co.uk (Alan Stokes)
Date: Tue, 17 May 94 12:54:21 GMT
Raw View
In <Cpu81A.Bt1@ucc.su.OZ.AU> maxtal@physics.su.OZ.AU (John Max Skaller) writes:
> Nothing is obvious. :-)

> Do two pointers to members to the same inline
>member in two translation units compare equal?
> Do pointers to inline static members compare equal?
The answer should be the same. It ought to be yes. C certainly says that
two pointers to the same function compare equal. I don't see anything in the
ARM & I'm not aware of anything in the draft standard that relaxes this rule
for inline functions.

> May an inline member refer to a static variable?
Definitely yes.

--
Alan Stokes (alan@rcp.co.uk)
Richards Computer Products Ltd
Didcot, UK




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sun, 15 May 1994 09:36:46 GMT
Raw View
In article <2qr2rhINN5ut@early-bird.think.com> barmar@think.com (Barry Margolin) writes:
>
>Inline functions are just like ordinary functions, except that the compiler
>is requested to optimize it better.  This affects linker behavior, but it
>should have no effect on the semantics of the function.  So inline
>functions use the same static binding as ordinary functions.  This should
>be pretty obvious when you consider inline member functions.

 Nothing is obvious. :-)

 Do two pointers to members to the same inline
member in two translation units compare equal?

 Do pointers to inline static members compare equal?

 May an inline member refer to a static variable?

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: barmar@think.com (Barry Margolin)
Date: 11 May 1994 16:58:25 GMT
Raw View
In article <2qe9mq$4b6@news.csus.edu> rbotting@wiley.csusb.edu (Dr. Richard Botting) writes:
>While teaching a comparison of programming languages class for nth
>time I noticed what should have been obvious - a macro and a function
>can access very different sets of global variables.  The macro collects
>those arround the call (socalled "dynamic bindiing").  The function binds
>its undeclared variables to those above the definition of the function (called
>static binding).

[Example deleted]

>I was wondering whether the standard for C++ will define which
>C++ inline functions will use in ANSI C++.

Inline functions are just like ordinary functions, except that the compiler
is requested to optimize it better.  This affects linker behavior, but it
should have no effect on the semantics of the function.  So inline
functions use the same static binding as ordinary functions.  This should
be pretty obvious when you consider inline member functions.

>As a related and supplementary question... In C, implicit in the
>operational semantics of macroes (but unnoticed in any text!) is the
>consequece that macros use call by name - an idea that was supposed to have
>died in 1968.   Again -  does the standard specify that
>inline functions in C++ use call by name?

Macros perform simple text (actually, preprocessor-token) substitution.
Both of your realizations about macros are straightforward consequences of
that definition.  And again, the difference in inline functions is a
consequence of the fact that they should act just like ordinary functions,
except the call should be faster (it's a time/space tradeoff).
--
Barry Margolin
System Manager, Thinking Machines Corp.

barmar@think.com          {uunet,harvard}!think!barmar




Author: rbotting@wiley.csusb.edu (Dr. Richard Botting)
Date: 6 May 1994 20:35:38 GMT
Raw View
While teaching a comparison of programming languages class for nth
time I noticed what should have been obvious - a macro and a function
can access very different sets of global variables.  The macro collects
those arround the call (socalled "dynamic bindiing").  The function binds
its undeclared variables to those above the definition of the function (called
static binding).  For example:
/* static and dynamic binding in C */
int x=10;
next_x(int delta) { return x + delta; }
#define NEXT_X(delta) x+delta
main(){
        int x=5;
        printf("%d, %d\n", next_x(4), NEXT_X(4));
}

I was wondering whether the standard for C++ will define which
C++ inline functions will use in ANSI C++.


As a related and supplementary question... In C, implicit in the
operational semantics of macroes (but unnoticed in any text!) is the
consequece that macros use call by name - an idea that was supposed to have
died in 1968.   Again -  does the standard specify that
inline functions in C++ use call by name?

My apologies if my ignorance of C++ has wasted bandwidth.

--
rbotting@wiley.csusb.edu.
rbotting::=`Dr. Richard J. Botting`, wiley::=`Faculty EMail System`,
csusb::=`California State University, San Bernardino, CA 92407, USA`.
Aka::=`dick@doc.csci.csusb.edu`.
Disclaimer::=`CSUSB may or may not agree with this message`.
Copyright(1994)::=`Copy and use as you wish as long as you include this
 copyright and signature`.
Ask me about our new Masters degree in Computer Science!