Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > External inline functions calling internal inline functions

Reply
Thread Tools

External inline functions calling internal inline functions

 
 
Daniel Vallstrom
Guest
Posts: n/a
 
      11-20-2003
Why does c99 require that inline functions with external linkage
must not refer to functions, possibly inline-specified, with
internal linkage? Here is the c99 text:

"6.7.4 Function specifiers
Syntax
function-specifier:
inline

Constraints
Function specifiers shall be used only in the declaration of an
identifier for a function. An inline definition of a function
with external linkage shall not contain a definition of a
modifiable object with static storage duration, and shall not
^^^^^^^^^^^^^
contain a reference to an identifier with internal linkage."
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^

Here is an example of what one might want to write:

<start file1.h>
inline int file1_f( int k );

<end file1.h>

<start file1.c>
static inline int g( int k )
{
return k+1;
}

inline int file1_f( int k )
{
return 2*k + g(k);
}

<end file1.c>


Why isn't this allowed? If the g call in file1_f is inlined
there is no problem? And if the g call isn't inlined why
can't the compiler choose to not inline file1_f calls, or
for that matter choose to inline file1_f calls and handle
the g call in some way? What's the problem?

I guess that compilers must give a diagnostic for constraint
violations but may produce a compiled program.

To be sure, when I run a program containing something like the
above example code through gcc and intel's icc, with -std=c99,
the program compiles just fine. gcc doesn't complain at all but
IIRC gcc states explicitly that their inline handling doesn't
follow the standard? icc gives the following warning:

"warning #1173: an entity with internal linkage cannot be
referenced within an inline function with external linkage"

but as said still compiles the program.


(I thought that this question must have been discussed
somewhere but I can't find an answer.)


Daniel Vallstrom
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      11-20-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Daniel Vallstrom) wrote in message news:<(E-Mail Removed). com>...
> Why does c99 require that inline functions with external linkage
> must not refer to functions, possibly inline-specified, with
> internal linkage? Here is the c99 text:
>
> "6.7.4 Function specifiers
> Syntax
> function-specifier:
> inline
>
> Constraints
> Function specifiers shall be used only in the declaration of an
> identifier for a function. An inline definition of a function
> with external linkage shall not contain a definition of a
> modifiable object with static storage duration, and shall not
> ^^^^^^^^^^^^^
> contain a reference to an identifier with internal linkage."
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^


The reason is that an inline definition of a function with external
linkage is meant to be the same function in all translation units,
whether or not it actually gets inlined. For any given identifier with
internal linkage, the inline definition of the function which used
that identifier would be defining a different function in each
translation unit. If you actually want it to be different in each
translation unit, it should either not be inline, or not have external
linkage, depending upon your reasons for doing things that way.

> Here is an example of what one might want to write:
>
> <start file1.h>
> inline int file1_f( int k );
>
> <end file1.h>
>
> <start file1.c>
> static inline int g( int k )
> {
> return k+1;
> }
>
> inline int file1_f( int k )
> {
> return 2*k + g(k);
> }
>
> <end file1.c>


Is there any reason why you couldn't correct the problem with this
code, by either adding 'static', or removing 'inline', from the
declaration of file1_f()?
 
Reply With Quote
 
 
 
 
Kevin Bracey
Guest
Posts: n/a
 
      11-21-2003
In message <(E-Mail Removed) >
(E-Mail Removed) (Daniel Vallstrom) wrote:

> Here is an example of what one might want to write:
>
> <start file1.h>
> inline int file1_f( int k );
>
> <end file1.h>
>
> <start file1.c>
> static inline int g( int k )
> {
> return k+1;
> }
>
> inline int file1_f( int k )
> {
> return 2*k + g(k);
> }
>
> <end file1.c>
>
>
> Why isn't this allowed? If the g call in file1_f is inlined
> there is no problem? And if the g call isn't inlined why
> can't the compiler choose to not inline file1_f calls, or
> for that matter choose to inline file1_f calls and handle
> the g call in some way? What's the problem?


It's well weird coding practice, what you've done there. And that's
kind of why it's disallowed. You wouldn't put the definition of
file_f in file1.c - you would normally put it in file1.h, so every function
including file1.h could have an inline definition.

Indeed, anyone just including file1.h as it stands would get a diagnostic
if they didn't provide their own inline definition...

So, if the inline definition (which would normally be in the header file)
used a static function, then that would normally indicate a programming
error, as it would imply that you weren't actually providing the same
functionality as the inline definition in another translation unit.

Now, let's say that file1_f is actually providing the external definition
of file_f, so you write:

extern inline int file1_f( int k )
{
return 2*k + g(k);
}

Now that would be legal. The external definition of file_f is allowed to
be different, and is allowed to reference static functions.

The diagnostic in question is a real pain to produce, as it doesn't
follow naturally from the code generation. Especially as you have to
defer generating it until you've read the whole file and know for certain
that you have an inline definition and not an external one.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cisco Nat Internal > External > Internal dcpearso Cisco 3 03-23-2008 07:01 AM
Access from internal hosts to internal servers using external address HangaS Cisco 2 04-19-2007 10:14 AM
cisco ios nat from internal->external->internal Bob Cisco 0 12-11-2006 01:05 PM
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
internal web service calling multiply types of external web servic Wendy Elizabeth ASP .Net Web Services 2 11-15-2004 10:57 PM



Advertisments