473,465 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

about extern inline and static inline

I am a little confused by the "extern inline and static inline" rules. I
understand that "extern inline" guarantees no function storage is
created (all are inlined). But the following test seems to contradict
what I expect---I found inc_i() is actually compiled as a linkable
function in the a.out
g++ main.cpp call_inc.cpp
nm a.out

......
0804875e W _Z5inc_iv

Can someone explain what's wrong with my understanding? Even better, I
would appreciate if you can show me the comprehensive rule of "static
inline" and "extern inline".

Thanks,
Sean

--------------------------------
//myheader.h
extern int i;
extern void call_inc_i();
extern inline void inc_i(){
i++;
}

-------------------------------
//main.cpp
#include <iostream>
#include "myheader.h"
using namespace std;
int i = 0;
int main(){
inc_i();
cout << "after inc_i()" << i << endl;
call_inc_i();
cout << "after call_inc_i()" << i << endl;
}

--------------------------------
//call_inc.cpp
#include "myheader.h"
void call_inc_i(){
inc_i();
}
--------------------------------------
Apr 29 '06 #1
4 7216
Sean wrote:
I am a little confused by the "extern inline and static inline" rules. I
understand that "extern inline" guarantees no function storage is
created (all are inlined).
This is incorrect in C++ (and I suspect it is an extension of gcc).
Inlining never guarantees anything (expect that the compiler will *try*
to inline it).
But the following test seems to contradict
what I expect---I found inc_i() is actually compiled as a linkable
function in the a.out
This is a normal behavior. Not all functions may be inlined. Compilers
are usually better than we are at inlining functions.
Can someone explain what's wrong with my understanding? Even better, I
would appreciate if you can show me the comprehensive rule of "static
inline" and "extern inline".


inline
tells the compiler to try to inline that function (it may refuse for
various reasons)

extern
redundant for a function but for an object, transforms a definition
into a declaration

static
for a namespace-scope name (not in a class), makes it local to the
translation
unit (roughly the file in which it is defined). However, this is
deprecated in C++,
unnamed namespaces should be used instead.

static inline and static extern are both combinations of the two terms.

Note that this does not apply to extern "C", which is a different
beast.
Jonathan

Apr 29 '06 #2
Jonathan Mcdougall wrote:
Sean wrote:
I am a little confused by the "extern inline and static inline" rules. I
understand that "extern inline" guarantees no function storage is
created (all are inlined).

This is incorrect in C++ (and I suspect it is an extension of gcc).
Inlining never guarantees anything (expect that the compiler will *try*
to inline it).

But the following test seems to contradict
what I expect---I found inc_i() is actually compiled as a linkable
function in the a.out

This is a normal behavior. Not all functions may be inlined. Compilers
are usually better than we are at inlining functions.

Can someone explain what's wrong with my understanding? Even better, I
would appreciate if you can show me the comprehensive rule of "static
inline" and "extern inline".

inline
tells the compiler to try to inline that function (it may refuse for
various reasons)

extern
redundant for a function but for an object, transforms a definition
into a declaration

static
for a namespace-scope name (not in a class), makes it local to the
translation
unit (roughly the file in which it is defined). However, this is
deprecated in C++,
unnamed namespaces should be used instead.

static inline and static extern are both combinations of the two terms.

but if extern inline means "extern + inline" (simple combination), there
exist two definition of incr_i() in my example--since myheader.h is
included in main.cpp and call_inc.cpp.
There must be some special meaning besides simple combination, I guess.

Note that this does not apply to extern "C", which is a different
beast.
Jonathan

Apr 29 '06 #3
Sean wrote:
Jonathan Mcdougall wrote:
Sean wrote:

inline
tells the compiler to try to inline that function (it may refuse for
various reasons)

extern
redundant for a function but for an object, transforms a definition
into a declaration

static
for a namespace-scope name (not in a class), makes it local to the
translation
unit (roughly the file in which it is defined). However, this is
deprecated in C++,
unnamed namespaces should be used instead.
I have to change that last sentence. Only using static on objects in a
namespace scope is deprecated. Using static on functions is legal.
static inline and static extern are both combinations of the two terms.
but if extern inline means "extern + inline" (simple combination), there
exist two definition of incr_i() in my example--since myheader.h is
included in main.cpp and call_inc.cpp.
There must be some special meaning besides simple combination, I guess.


No. As I said, extern for a function is redundant (a function always
has external linkage, unless it is static) so let's forget about it. If
a function is inline, its *definition* (body) is actually *required* in
each translation unit it used. Think: if the definition is not
available, how can it be inlined? Multiple definitions (in different
translation units) of an inline function is not only legal, but
required.

If the function is both static and inline, it may confuse the compiler,
which will probably decide not to inline the function. Because the
function is static, each translation unit will get its copy. If the
compiler honors the inline hint, each translation unit will have its
copy of the function inlined.
Jonathan

Apr 29 '06 #4
Sean wrote:
I am a little confused by the "extern inline and static inline" rules. I
understand that "extern inline" guarantees no function storage is
created (all are inlined).
No, it doesn't guarantee that at all. On the contrary. If a function is
extern, it must be linkable, so a non-inlined version must usually exist.
Using 'inline' doesn't really guarantee inlining at all. The only effect of
it regarding the C++ standard is that you can violate the
one-definition-rule. To the compiler, it's just a mere hint that you would
like the function to be inlined. The compiler might choose to ignore it, or
it might choose to inline functions that are not declared 'inline'.
But the following test seems to contradict what I expect---I found inc_i()
is actually compiled as a linkable
function in the a.out
>g++ main.cpp call_inc.cpp
>nm a.out

.....
0804875e W _Z5inc_iv

Can someone explain what's wrong with my understanding? Even better, I
would appreciate if you can show me the comprehensive rule of "static
inline" and "extern inline".


static means that the function can only be used in the translation unit
where it's defined. extern means it can be used in any translation unit of
the program. inline means that a function can be defined in multiple
translation units without an error.
Apr 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ik | last post by:
Hello all, Under C++ when 'extern' is used with out the string literal "C" does it act the same as a 'static' ? When is it appropriate using 'extern' ? Thanks ~Ik
2
by: evan | last post by:
Hi, I've got an easy one... I need to inline a few functions from one module to another. By looking at the compiled code I can see that the function is inlined if it is called from within the...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
9
by: JoeC | last post by:
I am trying to create a simple maze program on a window. I am trying to create a global var to handle the commands. I declare: #include "space.h" #include "player.h" #include "Command.h" ...
13
by: fctk | last post by:
source: http://rm-f.net/~orange/devel/specifications/c89-draft.html#3.1.2.2 there are two passages in this paragraph i can't fully understand: 1) "If the declaration of an identifier for an...
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
6
by: pookiebearbottom | last post by:
Let's say I have headers Sal.h with this class class Sal { public: int doit() { return 1;} }; now I know that the compilier can choose NOT to inline this function. So if I include this in...
7
by: Rahul | last post by:
Hi Everyone, I have the following code, void f(int &) { printf("in f...\n"); } #ifdef __cplusplus
5
by: gw7rib | last post by:
I was having linking errors when I put: const LPCTSTR Main_window_name = _TEXT("Thingy_main_window"); in one file and extern const LPCTSTR Main_window_name; in another. I've since...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.