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();
}
-------------------------------------- 4 7245
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 same module but not if it is called from a
second module i.e. extern inline.
For example (extremely simplified),
inline void Bob(void)
|
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 storage-class
specifier. My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing the
object defined in somewhere, and this "somewhere" object does not have
the storage-class specifier...
|
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"
static char gCmd = '$'; <--- My global command.
|
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 object or a function
contains the storage-class specifier extern , the identifier has the
same linkage as any visible declaration of the identifier with file
scope. If there is no visible declaration with file scope, the
identifier has external...
| |
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
|
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 two different libraries, they both choose to
|
by: Rahul |
last post by:
Hi Everyone,
I have the following code,
void f(int &)
{
printf("in f...\n");
}
#ifdef __cplusplus
|
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 realised that this is because (in C++) consts
do not, by default, have external linkage. I've solved the problem by
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |