473,654 Members | 3,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inline function

jg
Does C++ standard require an inline function be generated all the
time ?

For example,

#include <iostream>
using namespace std;

inline int foo()
{
return 10;
}

int main()
{
cout << "foo()=" << foo() << endl;
}

Should foo() be generated in the final executable all the time ?

JG

Jul 30 '07 #1
14 2317
On Jul 31, 8:51 am, jg <jgu...@gmail.c omwrote:
Does C++ standard require an inline function be generated all the
time ?
inline is just a hint or request of the compiler, and doesn't promise
anything...

Tony

Jul 31 '07 #2
On Jul 31, 4:51 am, jg <jgu...@gmail.c omwrote:
Does C++ standard require an inline function be generated all the
time ?
The correct term is "inlined", not "generated" .

Given that, the answer is that the standard doesnot require an inline
function to be inlined all the time.

-N
Jul 31 '07 #3
jg
On Jul 30, 8:47 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
On Jul 31, 4:51 am, jg <jgu...@gmail.c omwrote:Does C++ standard require an inline function be generated all the
time ?

The correct term is "inlined", not "generated" .

Given that, the answer is that the standard doesnot require an inline
function to be inlined all the time.

-N
No. I mean "generated" . Whether an inline function is inlined or not
is not my question. My question is whether a compiler will generate
the code for that inline function no matter whether the function
is inlined or not.

For my example, I tried g++. Without optimization, it does generate
foo(); with -O, it does not. With Sun Studio 11, it does not generate
foo() even without optimization.

JG

it does generate that

Jul 31 '07 #4
jg wrote:
On Jul 30, 8:47 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
>On Jul 31, 4:51 am, jg <jgu...@gmail.c omwrote:Does C++ standard require an inline function be generated all the
>>time ?
The correct term is "inlined", not "generated" .

Given that, the answer is that the standard doesnot require an inline
function to be inlined all the time.

-N

No. I mean "generated" . Whether an inline function is inlined or not
is not my question. My question is whether a compiler will generate
the code for that inline function no matter whether the function
is inlined or not.
The answer has to be yes, if the function is used. Code will either be
generated inline where it is called, or elsewhere if the compiler
decides not to inline the function.

There is no requirement to generate a stand alone function.

--
Ian Collins.
Jul 31 '07 #5
On Jul 31, 10:12 am, Ian Collins <ian-n...@hotmail.co mwrote:
jg wrote:
On Jul 30, 8:47 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
On Jul 31, 4:51 am, jg <jgu...@gmail.c omwrote:Does C++ standard require an inline function be generated all the
time ?
The correct term is "inlined", not "generated" .
Given that, the answer is that the standard doesnot require an inline
function to be inlined all the time.
-N
No. I mean "generated" . Whether an inline function is inlined or not
is not my question. My question is whether a compiler will generate
the code for that inline function no matter whether the function
is inlined or not.

The answer has to be yes, if the function is used. Code will either be
generated inline where it is called, or elsewhere if the compiler
decides not to inline the function.

There is no requirement to generate a stand alone function.

--
Ian Collins.
Hi Jg,
It is up to the compiler based on the size of the piece of
inline code constructed by the programmer.
If the compiler feels not to make the piece as inline(because of
larger size), it does not generate the inline code.

Thanks,
nalla

Jul 31 '07 #6
On Jul 31, 1:51 am, jg <jgu...@gmail.c omwrote:
Does C++ standard require an inline function be generated all the
time ?
For example,
#include <iostream>
using namespace std;
inline int foo()
{
return 10;
}
int main()
{
cout << "foo()=" << foo() << endl;
}
Should foo() be generated in the final executable all the time ?
I'm not sure what you mean by "generated" . The language defines
the semantics of a given C++ program (provided there is no
undefined behavior). Those semantics result in one or more
possible "observable behavior". Basically, the language
standard then says that a compiler can do whatever it wants, as
long as executing the resulting program results in one of the
observable behaviors.

On a Unix, machine, for example, for the above a compiler could
legally generate something like:
write( 1, "foo()=10\n ", 9 ) ;
return 0 ;
Most compilers will probably generate something like:
cout << "foo()=" << 10 << endl ;
return 0 ;
for the above.

Some compilers might also generate an out of line copy of foo(),
perhaps to simplify debugging (e.g. if you've asked for
debugging information). What the compiler generates, however,
doesn't matter as long as executing the code results in an
observable behavior which corresponds to the specified
semantics.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 31 '07 #7
na********@gmai l.com wrote:
On Jul 31, 10:12 am, Ian Collins <ian-n...@hotmail.co mwrote:
>jg wrote:
>>On Jul 30, 8:47 pm, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
On Jul 31, 4:51 am, jg <jgu...@gmail.c omwrote:Does C++ standard require an inline function be generated all the
time ?
The correct term is "inlined", not "generated" .
Given that, the answer is that the standard doesnot require an inline
function to be inlined all the time.
-N
No. I mean "generated" . Whether an inline function is inlined or not
is not my question. My question is whether a compiler will generate
the code for that inline function no matter whether the function
is inlined or not.
The answer has to be yes, if the function is used. Code will either be
generated inline where it is called, or elsewhere if the compiler
decides not to inline the function.

There is no requirement to generate a stand alone function.

--
Ian Collins.

Hi Jg,
It is up to the compiler based on the size of the piece of
inline code constructed by the programmer.
If the compiler feels not to make the piece as inline(because of
larger size), it does not generate the inline code.
I think you are all being a bit obtuse here. He is clearly asking
whether a (non-inlined) subroutine for the function will be generated
in the final binary regardless of whether the compiler inlined its
contents in the only place where it was called or not.

Or if we word it in another way: Even if the compiler inlined the
contents of the function at the calling location, will the contents
of the function also exist as a non-inlined function in the final
binary (even if this non-inlined version is never called anywhere)?

I don't know what the standard says, but IIRC at least gcc has a
command-line option to force it to create non-inlined instances of
inline functions regardless of whether they are necessary or not.
Jul 31 '07 #8
jg <jg****@gmail.c omwrote in news:1185839484 .488728.323430
@b79g2000hse.go oglegroups.com:
Does C++ standard require an inline function be generated all the
time ?

For example,

#include <iostream>
using namespace std;

inline int foo()
{
return 10;
}

int main()
{
cout << "foo()=" << foo() << endl;
}

Should foo() be generated in the final executable all the time ?
Not necessarily. If the function is declared as inline, the function body
may never exist in the final executable. There are certain actions which
will guarantee that it does exist (like taking it's address), and you can't
guarantee that the function body will _not_ be in the executable.
Jul 31 '07 #9
jg
Thanks for all answers.

To summarize, C++ standard does not require that the out-of-line code
of an inline function be generated all the time. It looks like C++
standard does not have explicit wording about this (at least I don't
find one); and I take it as whether to generate out-of-line code is a
up to an implementation.

The reason I was asking this is that in C Standard (C99), a compiler
must not generate the out-of-line code of an inline function, even
there
are calls to that inline function and those calls are not inlined.


Aug 1 '07 #10

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

Similar topics

13
6554
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
14
2771
by: Chris Mantoulidis | last post by:
I am not clear with the use of the keyword inline... I believe you add it do a function when you implement the function inside the header file where the class is stored... But is that all? What am I missing? If that's all, then why did Bjarne even bother adding it to the language? If that's not all, what else can I do with "inline"?
47
3852
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
20
3134
by: Grumble | last post by:
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?
5
1936
by: Tony Johansson | last post by:
Hello experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that sound strange. Here is the whole section: It says" Because inline functions are expanded at compile time, definitions of these functions, unlike other definitions cannot be separately compiled and must be
6
3996
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
18
5050
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?
9
2655
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,
7
16097
by: Wu Shaohua | last post by:
Hi Guys, 1. As we know usually we should not define a constructor as inline. I also learned if we define a member function inside the class this member function will be automatically be inline'ed. My question is: If I define a constructor (including its body) or another large member function inside the class, the constructor or the member function is inline or not? why? 2. I learned that if the member function is big we should not...
12
676
by: sam_cit | last post by:
Hi Everyone, I have few questions on inline functions, when i declare a function as inline, is it for sure that the compiler would replace the function call with the actual body of the function? or is it a call taken by compiler? Second, i see that it is same as what Macro's used to do for c, if so what is the advantage for going in for inline functions than to Macros?
0
8285
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
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, 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...
0
8706
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...
0
8591
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 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...
0
7304
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5621
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
4149
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...
1
2709
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
2
1592
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.