External inline functions calling internal inline functions

D

Daniel Vallstrom

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
 
J

James Kuyper

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()?
 
K

Kevin Bracey

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

inline functions 50
Inline functions and warning 7
Inline Functions? 3
about inline functions 14
Inline functions and linkage 5
inline vs. function pointers 36
gcc inline memcpy 7
inline + va_list 4

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top