![]() |
External inline functions calling internal inline functions
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 |
Re: External inline functions calling internal inline functions
Daniel.Vallstrom@safelogic.se (Daniel Vallstrom) wrote in message news:<171aec1a.0311191643.6a53aebb@posting.google. 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()? |
Re: External inline functions calling internal inline functions
In message <171aec1a.0311191643.6a53aebb@posting.google.com >
Daniel.Vallstrom@safelogic.se (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/ |
| All times are GMT. The time now is 02:32 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.