473,614 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inline keyword and linkage

Hello everyone,

As far as I understand, the 'inline' keyword is a hint for the compiler
to consider the function in question as a candidate for inlining, yes?

What happens when a function with extern linkage is inlined? Should the
compiler still export the function?

Or is an inlined function implicitly static?

Is extern inline foo() { } a legal function defintion?

Is inline foo(...) { } different from inline foo() { } ?

I was somewhat confused after reading
http://cpptips.hyperformix.com/cpptips/extern_inline3

$ cat foo.cxx
extern inline int foo(int n)
{
int sum = 0;
for (int i = 0; i < n; ++i) sum += i;
return sum;
}

$ g++-3.4.3 -std=c++98 -Wall -Wextra -pedantic -c foo.cxx

$ nm foo.o
/* NO SYMBOLS EXPORTED */

--
Regards, Grumble
Jul 23 '05 #1
20 3132
Grumble wrote:
Hello everyone,

As far as I understand, the 'inline' keyword is a hint for the compiler
to consider the function in question as a candidate for inlining, yes?

What happens when a function with extern linkage is inlined? Should the
compiler still export the function?

Or is an inlined function implicitly static?

Is extern inline foo() { } a legal function defintion?

Is inline foo(...) { } different from inline foo() { } ?


As it is mentioned in TC++PL 3 on page 199:
"An inline function (7.1.1, 10.2.9) must be defined – by identical
definitions (9.2.3) – in every translation unit in which it is used.

Consequently, the following example isn't just bad taste; it is illegal:

// file1.c:
inline int f(int i) { return i; }
// file2.c:
inline int f(int i) { return i+1; }
Unfortunately, this error is hard for an implementation to catch, and
the following – otherwise perfectly logical – combination of external
linkage and inlining is banned to make life simpler for compiler writers:
// file1.c:
extern inline int g(int i);

int h(int i) { return g(i); } // error: g() undefined in this
//translation unit

// file2.c:
extern inline int g(int i) { return i+1; }"


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #2

"Grumble" <de*****@kma.eu .org> wrote in message
news:cu******** **@news-rocq.inria.fr.. .
Hello everyone,

As far as I understand, the 'inline' keyword is a hint for the compiler
to consider the function in question as a candidate for inlining, yes?
Yes.

What happens when a function with extern linkage is inlined?
Nothing special.
Should the
compiler still export the function?
Unless it's explicitly specified as static, it has
external linkage, whether it's declared inline or not.
But there is a further requirement about an inline
function *definition*, cited below.
Or is an inlined function implicitly static?
No.

Is extern inline foo() { } a legal function defintion?
Yes.

Is inline foo(...) { } different from inline foo() { } ?
Yes, the signatures are different.

I was somewhat confused after reading
http://cpptips.hyperformix.com/cpptips/extern_inline3

$ cat foo.cxx
extern inline int foo(int n)
{
int sum = 0;
for (int i = 0; i < n; ++i) sum += i;
return sum;
}

$ g++-3.4.3 -std=c++98 -Wall -Wextra -pedantic -c foo.cxx

$ nm foo.o
/* NO SYMBOLS EXPORTED */


=============== =============== =============== =============== ====
ISO/IEC 14882:1998(E)

.....

3.2 One definition rule

3 Every program shall contain exactly one definition of every
non*inline function or object that is used in that program;
no diagnostic required. The definition can appear explicitly
in the program, it can be found in the standard or a user-
*defined library, or (when appropriate) it is implicitly defined
(see 12.1, 12.4 and 12.8). An inline function shall be defined
in every translation unit in which it is used.

.....

3.5 Program and linkage

3 A name having namespace scope (3.3.5) has internal linkage
if it is the name of
-- an object, reference, function or function template that
is explicitly declared static or,
-- an object or reference that is explicitly declared const
and neither explicitly declared extern nor previously
declared to have external linkage; or
-- a data member of an anonymous union.

4 A name having namespace scope has external linkage if it is
the name of
-- an object or reference, unless it has internal linkage; or
-- a function, unless it has internal linkage; or
-- a named class (clause 9), or an unnamed class defined in
a typedef declaration in which the class has the
typedef name for linkage purposes (7.1.3); or
-- a named enumeration (7.2), or an unnamed enumeration defined
in a typedef declaration in which the
enumeration has the typedef name for linkage purposes
(7.1.3); or
-- an enumerator belonging to an enumeration with external
linkage; or
-- a template, unless it is a function template that has internal
linkage (clause 14); or
-- a namespace (7.3), unless it is declared within an unnamed
namespace.

.....

7.1.2 Function specifiers

4 An inline function shall be defined in every translation
unit in which it is used and shall have exactly the same
definition in every case (3.2). [Note: a call to the inline
function may be encountered before its defi*nition appears
in the translation unit. ] If a function with external
linkage is declared inline in one transla*tion unit, it
shall be declared inline in all translation units in which
it appears; no diagnostic is required. An inline function
with external linkage shall have the same address in all
translation units. A static local variable in an extern
inline function always refers to the same object. A string
literal in an extern inline function is the same object in
different translation units.
=============== =============== =============== =============== ====

-Mike
Jul 23 '05 #3
"Grumble" <de*****@kma.eu .org> wrote in message
news:cu******** **@news-rocq.inria.fr.. .
Hello everyone, Hi, As far as I understand, the 'inline' keyword is a hint for the compiler
to consider the function in question as a candidate for inlining, yes? Yes.
What happens when a function with extern linkage is inlined? A function declared as "extern inline" is only guaranteed to
have the same address in all translation units that take that
address.
Should the compiler still export the function? Not necessarily.
Or is an inlined function implicitly static? No, as far as I understand: the function may still have
linkage (and its name be exported).
Is extern inline foo() { } a legal function defintion? Yes.
Is inline foo(...) { } different from inline foo() { } ? Yes, inline functions can be overloaded.
I was somewhat confused after reading
http://cpptips.hyperformix.com/cpptips/extern_inline3


The situation is a bit complex, because implementors
have some freedom in how they implement inline functions.
FYI, here's the exact wording of the standard (7.1.2/2-4):

A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares
an inline function. The inline

specifier indicates to the implementation that inline substitution of the
function body at the point of call is

to be preferred to the usual function call mechanism. An implementation is
not required to perform this

inline substitution at the point of call; however, even if this inline
substitution is omitted, the other rules for

inline functions defined by 7.1.2 shall still be respected.

A function defined within a class definition is an inline function. The
inline specifier shall not appear

on a block scope function declaration.79)

An inline function shall be defined in every translation unit in which it is
used and shall have exactly the

same definition in every case (3.2). [Note: a call to the inline function
may be encountered before its definition

appears in the translation unit. ] If a function with external linkage is
declared inline in one translation

unit, it shall be declared inline in all translation units in which it
appears; no diagnostic is required. An

inline function with external linkage shall have the same address in all
translation units. A static

local variable in an extern inline function always refers to the same
object. A string literal in an

extern inline function is the same object in different translation units.

hth
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Jul 23 '05 #4
Grumble wrote:
Is inline foo(...) { } different from inline foo() { } ?


I meant:

Is "inline foo(...) { }" different from "extern inline foo(...) { }" ?

(Two mistakes in a single line, Doh!)

--
Regards, Grumble
Jul 23 '05 #5
Ivan Vecerina wrote:
The situation is a bit complex, because implementors
have some freedom in how they implement inline functions.
FYI, here's the exact wording of the standard (7.1.2/2-4):

A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares
an inline function. The inline

specifier indicates to the implementation that inline substitution of the
function body at the point of call is

to be preferred to the usual function call mechanism. An implementation is
not required to perform this

inline substitution at the point of call; however, even if this inline
substitution is omitted, the other rules for

inline functions defined by 7.1.2 shall still be respected.

A function defined within a class definition is an inline function. The
inline specifier shall not appear

on a block scope function declaration.79)

An inline function shall be defined in every translation unit in which it is
used and shall have exactly the

same definition in every case (3.2). [Note: a call to the inline function
may be encountered before its definition

appears in the translation unit. ] If a function with external linkage is
declared inline in one translation

unit, it shall be declared inline in all translation units in which it
appears; no diagnostic is required. An

inline function with external linkage shall have the same address in all
translation units. A static

local variable in an extern inline function always refers to the same
object. A string literal in an

extern inline function is the same object in different translation units.


So the following from TC++PL does not apply, or BS means something else
with it?
"Unfortunat ely, this error is hard for an implementation to catch, and
the following – otherwise perfectly logical – combination of external
linkage and inlining is banned to make life simpler for compiler writers:
// file1.c:
extern inline int g(int i);

int h(int i) { return g(i); } // error: g() undefined in this
//translation unit

// file2.c:
extern inline int g(int i) { return i+1; }"

extern inline is not banned in general, or the specific use (which is?)
is only banned?


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #6
Grumble wrote:
...
As far as I understand, the 'inline' keyword is a hint for the compiler
to consider the function in question as a candidate for inlining, yes?
Yes.
What happens when a function with extern linkage is inlined? Should the
compiler still export the function?
Maybe. Remember, that there are contexts where inline functions cannot
be actually inlined. For example, if you take the address of that
function, store it in a pointer and then, later, cal it through that
pointer. In general case, in order to serve this call the compiler has
no other choice but to generate a "regular" (non-inlined ) body for this
function. Now, if this function has external linkage, the compiler has
to make sure that all such pointers obtained in all translation units
have the same value (even though every translation units is required to
include it's own copy of the definition of the inline function). In
order to achieve that the compiler has to establish some form of
"communicat ion" between translation units. Traditionally, this is done
by exporting the inline function from all modules where it has a
"regular" body. Later, the linker will choose one and only one of the
exported bodies for each function and discard all others.

Note, that the above exporting is done for a purely internal purpose. It
does not mean that the user is allowed to "link" to that exported
function from other translation units by providing a mere declaration of
the function there

--- file1.c ---

inline void foo() {}

--- file2.c ---

void foo();

int main() {
foo();
// Might compile and "work" in practice, but is not supported by
// the language. The behavior is undefined
}

----------------

The compiler is free prevent the above code from compiling (for example,
by using a dedicated name mangling convention for exported inline
function bodies, different from the convention used with regular functions).
Or is an inlined function implicitly static?
No. Inline functions are have external linkage by default, just like
regular functions.
Is extern inline foo() { } a legal function defintion?
Yes. But see above.
Is inline foo(...) { } different from inline foo() { } ?
Hm... Yes. The argument declarations are different. But in this case the
difference has nothing to do with inlining.
I was somewhat confused after reading
http://cpptips.hyperformix.com/cpptips/extern_inline3

$ cat foo.cxx
extern inline int foo(int n)
This 'extern' is superfluous.
{
int sum = 0;
for (int i = 0; i < n; ++i) sum += i;
return sum;
}

$ g++-3.4.3 -std=c++98 -Wall -Wextra -pedantic -c foo.cxx

$ nm foo.o
/* NO SYMBOLS EXPORTED */


Not surprising. In this translation unit you are not given the compiler
any reason to generate a regular body for the inline function. Since
there's no body, there's nothing to export.

Try taking the address of the function and using it in some non-trivial
fashion. See what will happen. Something like this

inline int foo(int n)
{
int sum = 0;
for (int i = 0; i < n; ++i) sum += i;
return sum;
}

int bar(int i, int (*fn)(int))
{
return (*fn)(i);
}

int baz()
{
return bar(100, &foo);
}

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #7
Andrey Tarasevich wrote:

Sorry for multiple typos in my previous post
be actually inlined. For example, if you take the address of that
function, store it in a pointer and then, later, cal it through that ^^^^
call ...
have the same value (even though every translation units is required to ^^^^^
unit ...
The compiler is free prevent the above code from compiling (for example, ^^
to ...
Or is an inlined function implicitly static?
No. Inline functions are have external linkage by default, just like

^^^^^^^^^^^^^^^ ^^^
functions have ...
Not surprising. In this translation unit you are not given the compiler

^^^
have

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #8

"Ioannis Vranos" <iv*@remove.thi s.grad.com> wrote in message
news:1108405964 .548619@athnrd0 2...
Ivan Vecerina wrote:
The situation is a bit complex, because implementors
have some freedom in how they implement inline functions.
FYI, here's the exact wording of the standard (7.1.2/2-4):

A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function. The inline

specifier indicates to the implementation that inline substitution of the function body at the point of call is

to be preferred to the usual function call mechanism. An implementation is not required to perform this

inline substitution at the point of call; however, even if this inline
substitution is omitted, the other rules for

inline functions defined by 7.1.2 shall still be respected.

A function defined within a class definition is an inline function. The
inline specifier shall not appear

on a block scope function declaration.79)

An inline function shall be defined in every translation unit in which it is used and shall have exactly the

same definition in every case (3.2). [Note: a call to the inline function may be encountered before its definition

appears in the translation unit. ] If a function with external linkage is declared inline in one translation

unit, it shall be declared inline in all translation units in which it
appears; no diagnostic is required. An

inline function with external linkage shall have the same address in all
translation units. A static

local variable in an extern inline function always refers to the same
object. A string literal in an

extern inline function is the same object in different translation
units.
So the following from TC++PL does not apply, or BS means something else
with it?
Yes it does apply.
"Unfortunat ely, this error is hard for an implementation to catch, and
the following – otherwise perfectly logical – combination of external
linkage and inlining is banned to make life simpler for compiler writers:
// file1.c:
extern inline int g(int i);

int h(int i) { return g(i); } // error: g() undefined in this
//translation unit
The rules banning this are 3.2/3 and 7.1.2/4, which I cited
in my post elsethread.

// file2.c:
extern inline int g(int i) { return i+1; }"

extern inline is not banned in general,
No.
or the specific use (which is?)
is only banned?


What is banned is the use of an extern declaration in
place of a definition in the translation unit from where it
is called.
IMO the simplest way to deal with this is if you want to define
a nonmember function as inline, define it in a header, and
include the header in translation units which call it.

-Mike


Jul 23 '05 #9

"Grumble" <de*****@kma.eu .org> wrote in message
news:cu******** **@news-rocq.inria.fr.. .
Grumble wrote:
Is inline foo(...) { } different from inline foo() { } ?


I meant:

Is "inline foo(...) { }" different from "extern inline foo(...) { }" ?

(Two mistakes in a single line, Doh!)


No, they're not different if declared at namespace scope,
because in that case 'extern' is implied; the specifier
is redundant.

-Mike

Jul 23 '05 #10

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

Similar topics

13
6547
by: A | last post by:
Hi, I'm having problems completing a project in C++. I have been using inline functions in some of my header files. I have only done so for simple functions that only have 1 statement (eg. accessor and mutator methods to access private data members). I would like to know if there are any issues with using inline functions that may have attributed to my errors before I start separting them out into "outline functions". Regards
47
3846
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
6
2447
by: John Ratliff | last post by:
I was reading the C++ FAQ Lite about inline functions, and it says the following (http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.7) " It's usually imperative that the function's definition (the part between the {...}) be placed in a header file. If you put the inline function's definition into a .cpp file, and if it is called from some other .cpp file, you'll get an "unresolved external" error from the linker. " Why...
9
6354
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
18
5046
by: Method Man | last post by:
If I don't care about the size of my executable or compile time, is there any reason why I wouldn't want to inline every function in my code to make the program run more efficient?
2
2690
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)
3
6495
by: Bilgehan.Balban | last post by:
Hi, My observation was that a function with `inline' qualifier has file scope in C++ and it's symbol is not exported. Contrary to this, in C an `inline' function symbol is exported, unless it also has `static' or `extern' qualifiers. How could I achieve the C behaviour in C++, in that an inline function symbol is exported? I see that one way to do it is declaring the function in a class definition in a header file so that all files
9
2654
by: Bilgehan.Balban | last post by:
Hi, If I define an inline function in one .c file, and use it from another, after compiling and linking the two, it seems the function is not inlined but rather called as a regular function. I would expect to see it inlined during linkage of the two object files. Does inlining only occur if the inline function is defined within the same file that it is called? Thanks,
2
3766
by: Nagrik | last post by:
Dear Group, The book of Bjarne Stroustrup in chapter 5.4.4 says the following "The word static is one of the most overused words in C and C++. For static data members it has both of the common meanings: static as in "statically allocated" as opposed to on the stack or on the free store and static as in "with restricted visibility" as opposed to with external linkage. For member functions, static has the second meaning."
0
8179
marktang
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...
0
8576
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 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...
1
8272
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5538
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();...
0
4049
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...
0
4119
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2565
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 we have to send another system
1
1712
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1421
bsmnconsultancy
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...

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.