473,666 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sum up the effects of "inline"?


The original purpose of "inline" was that code could be "expanded in
place". Of course, it has other uses...

For one thing, the following two translation units will compile together
succesfully into a program:

/* source1.cpp */

inline int Func(double arg)
{
static double d = 0.;

return d += arg;
}

int main()
{
Func( 24.333);
}
/* source2.cpp */

inline int Func(double arg)
{
static double d = 0.;

return d += arg;
}
Here, "inline" alleviates our need to abide by the "One Definition Rule".

I have two questions though:

(1) What would be the effect of putting "extern" or "static" before
an inline function? Do inline functions have internal linkage, external
linkage, or neither?

(2) Template functions too seem to alleviate our need to abide by the
"One Definition Rule". Is this true? What would be the effect of putting
"inline", "extern" or "static" before a template function?
--

Frederick Gotham
Jun 30 '06 #1
14 2103
> Here, "inline" alleviates our need to abide by the "One Definition Rule".

No, becoz the body gets substituted as if there is no function.
(1) What would be the effect of putting "extern" or "static" before
an inline function? Do inline functions have internal linkage, external
linkage, or neither?
Read C++-FAQs.
(2) Template functions too seem to alleviate our need to abide by the
"One Definition Rule". Is this true?
NO, template functions gives u different functions upon instantiations.
What would be the effect of putting
"inline", "extern" or "static" before a template function?


compiler error

Jun 30 '06 #2
Chandu posted:
Here, "inline" alleviates our need to abide by the "One Definition
Rule".


No, becoz the body gets substituted as if there is no function.

Shut up.

(1) What would be the effect of putting "extern" or "static"
before
an inline function? Do inline functions have internal linkage,
external linkage, or neither?


Read C++-FAQs.

Shut up.

(2) Template functions too seem to alleviate our need to abide by
the
"One Definition Rule". Is this true?


NO, template functions gives u different functions upon
instantiations.

Shut up.

What would be the effect of putting
"inline", "extern" or "static" before a template function?


compiler error

Shut up.

Four wrong answers, each answered with a more authoritive tone than the
last.

I think this is my first time using a killfile.

--

Frederick Gotham
Jun 30 '06 #3

Frederick Gotham wrote:
Shut up.
Wow, remind me never to help you.

Four wrong answers, each answered with a more authoritive tone than the
last.
Actually, one answer that is dubious, one that is incorrect, one that
is correct, and a suggestion that did not answer any question.

I think this is my first time using a killfile.


You're well on your way.

Jun 30 '06 #4
Frederick Gotham posted:

(1) What would be the effect of putting "extern" or "static"
before
an inline function? Do inline functions have internal linkage,
external linkage, or neither?

Okay I've done some experimenting.

My conclusions are as follows:

(1) "inline" functions have external linkage by default.

(2) Placing "extern" before an inline function has no effect, because
it's already "extern" by default.

(3) Placing "static" before an inline function, however, certainly does
have an effect -- but I think the effect is only noticable if the inline
function contains static local objects.

Could someone please confirm that these assertions are right?

I have come across an interesting scenario however, involving an extern
inline function. Here's a short program consisting of three files:

/* header.hpp */

#ifndef INCLUDE_HEADER_ HPP
#define INCLUDE_HEADER_ HPP

inline int InlineFunc()
{
return ++val; /* Alters a global variable */
}

#endif

/* END OF header.hpp */
/* main.cpp */

static int val = 0;

#include "header.hpp "
void FuncInOtherFile ();
#include <iostream>

int main()
{
FuncInOtherFile ();

std::cout << InlineFunc() << '\n';
}

/* END OF main.cpp */
/* OtherFile.cpp */

static int val = 0;

#include "header.hpp "

void FuncInOtherFile ()
{
InlineFunc();
}

/* END OF OtherFile.cpp */
Is there anything in the Standard which forbids an extern inline function
from accessing a global static object? Judging by the above program, it
appears to me that there should be such a restriction. (However no such
restriction would be necessary for a static inline function).
--

Frederick Gotham
Jun 30 '06 #5
Frederick Gotham posted:

(2) Template functions too seem to alleviate our need to abide by
the
"One Definition Rule". Is this true? What would be the effect of
putting "inline", "extern" or "static" before a template function?

I have experimented with template functions aswell. My conclusions are as
follows:

(1) A template function has external linkage by default.
(2) Placing "extern" before it has no effect, because it's already
extern by default.
(3) Placing "static" before it gives it internal linkage, and has a
noticable effect if the function contains static local objects.
(4) Placing "inline" before a template function has no effect other
than to ask the compiler to expand the code in-place.
Okay... so I've learned something. I'll go experiment with "export" now.

--

Frederick Gotham
Jun 30 '06 #6

"Frederick Gotham" <fg*******@SPAM .com> skrev i meddelandet
news:05******** ***********@new s.indigo.ie...
Frederick Gotham posted:

(1) What would be the effect of putting "extern" or "static"
before
an inline function? Do inline functions have internal linkage,
external linkage, or neither?

Okay I've done some experimenting.

My conclusions are as follows:

(1) "inline" functions have external linkage by default.

(2) Placing "extern" before an inline function has no effect,
because
it's already "extern" by default.

(3) Placing "static" before an inline function, however,
certainly does
have an effect -- but I think the effect is only noticable if the
inline
function contains static local objects.

Could someone please confirm that these assertions are right?

I have come across an interesting scenario however, involving an
extern
inline function. Here's a short program consisting of three files:

/* header.hpp */

#ifndef INCLUDE_HEADER_ HPP
#define INCLUDE_HEADER_ HPP

inline int InlineFunc()
{
return ++val; /* Alters a global variable */
}

#endif

/* END OF header.hpp */
/* main.cpp */

static int val = 0;

#include "header.hpp "
void FuncInOtherFile ();
#include <iostream>

int main()
{
FuncInOtherFile ();

std::cout << InlineFunc() << '\n';
}

/* END OF main.cpp */
/* OtherFile.cpp */

static int val = 0;

#include "header.hpp "

void FuncInOtherFile ()
{
InlineFunc();
}

/* END OF OtherFile.cpp */
Is there anything in the Standard which forbids an extern inline
function
from accessing a global static object? Judging by the above program,
it
appears to me that there should be such a restriction.


No, there are not.

There are however rules against including the header files so that
different 'val' variables are visible to the function definitions.
That is a violation of the One Definition Rule.

(However no such
restriction would be necessary for a static inline function).


In that case they are technically different functions, that just
happen to have the same name. This is ok, as they are local functions
in different compilation units.
Bo Persson
Jun 30 '06 #7
Frederick Gotham posted:
Okay... so I've learned something. I'll go experiment with "export"
now.

I've always thought of "external linkage of a function" implying two
things:

(1) The function can be invoked from inside a different source file,
even though the other source file only contains a declaration of the
function, rather than a definition.
(2) If an external linkage function contains static local objects,
they're shared by the entire program (rather than having a unique set of
static objects for each translation unit).

An ordinary template function such as the following has external linkage:

template<class T>
extern T TemplateFunc()
{
static T val = 0;

return ++val;
}

(The "extern" is superfluous, and so it can be written simply as:)

template<class T>
T TemplateFunc()
{
static T val = 0;

return ++val;
}
Given that the template function immediately above has external linkage,
it satisfies point (2) which I mention above. However, it doesn't satisfy
point (1), as demonstrated by the following program:
/* main.cpp */

template<class T>
T TemplateFunc();

int main()
{
TemplateFunc<in t>();
}
/* otherfile.cpp */

template<class T>
T TemplateFunc()
{
static T val = 0;

return ++val;
}
So it doesn't seem quite fitting to say that a template function has
"external linkage", because it doesn't satisfy point (1) that I mention
at the beginning of this post.

It seems to me that if you want a template function to have TRUE external
linkage, then you have to define it as "export". Unfortunately I don't
have a compiler which supports "export", so I can't test it.
--

Frederick Gotham
Jun 30 '06 #8
Frederick Gotham <fg*******@SPAM .com> wrote:
I've always thought of "external linkage of a function" implying two
things:

(1) The function can be invoked from inside a different source file,
even though the other source file only contains a declaration of the
function, rather than a definition.
(2) If an external linkage function contains static local objects,
they're shared by the entire program (rather than having a unique set of
static objects for each translation unit).


Under what conditions would a static object declared within
a function exist separately for each translation unit specifically?

Steve
Jun 30 '06 #9
Steve Pope posted:

(2) If an external linkage function contains static local objects,
they're shared by the entire program (rather than having a unique set ofstatic objects for each translation unit).


Under what conditions would a static object declared within
a function exist separately for each translation unit specifically?

Here's a little program for you. It consists of three files, a header and
two source files.

The header file contains a vanilla inline function.

/* header.hpp */

#ifndef INCLUDE_HEADER_ HPP
#define INCLUDE_HEADER_ HPP

inline unsigned InlineFunc()
{
static unsigned i = 0;

return ++i;

}

#endif
/* a.cpp */

#include "header.hpp "

void FuncInOtherFile ();

#include <iostream>

int main()
{
FuncInOtherFile ();

std::cout << InlineFunc() << '\n';

std::getchar();
}
/* b.cpp */

#include "header.hpp "

void FuncInOtherFile ()
{
InlineFunc();
}
Run the program and see what output you get.

Then change the inline function to "static", re-compile, and see what
output you get. (Some compilers might need you to "Rebuild All" because
they don't notice that the header has been changed...)

The moral of the story is that there's more than one function. There's
the static function in "a.cpp", and there's the static function in
"b.cpp", and they're unrelated as far as the compiler and linker are
concerned.

--

Frederick Gotham
Jun 30 '06 #10

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

Similar topics

14
2772
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"?
9
2057
by: John Rambo | last post by:
Hi, I made the following test program: //////////////////////// classes_1.cpp #include <iostream> #include "classes_1.h" using namespace std; A::A():i(0){cout <<"const A: i =" << i <<endl;} A::~A(){cout <<"destr A"<<endl;} inline void A::showA() {cout << "show A: i =" << i <<endl;}; //////////////////////// classes_1.h
10
3305
by: Christian Staudenmayer | last post by:
Hi, is there any revision of the C standard that allows the "inline" keyword (or a similar feature)? I know it is possible in gcc, but then it might be a gcc feature only. Greetings, Chris -- Christian Staudenmayer
2
8681
by: Steve Richter | last post by:
I would like to use display:inline and other CSS attributes to build an entry form. Where the heading to the left of the text box is always a set width. It is not working so I am experimenting with two divs to get them to render side by side: <div style="width:12em;border: 2px solid #EFCE8C; padding: 0.5em;display:inline;">abc</div> <div style="width:12em;border: 2px solid #EFCE8C; padding:
12
11535
by: Dave H. | last post by:
Please redirect me if this message is posted to the wrong group. Given the intention of delivering content to an HTTP user agent (such as Internet Explorer) which is to be immediately opened by an associated application, there are the Content-Disposition type options of "inline" or "extension-token". Specifically as regards Internet Explorer, I've tried both inline and the specific filename extension (xls,csv,pdf,doc,...) and the...
3
3142
by: Baron Samedi | last post by:
I am looking for a reliable cross-browser pull-quote solution in CSS. See for instance, http://www.sitepoint.com/examples/pullquotes/ The problem seems at first to be sure that it functions across all browsers, but there seem to be a few very similar solutions, so that is probably not a major hurdle. However, just to complicate things, I want to have it "inline" (http:// www.tizag.com/cssT/inline.php)
17
8369
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
8355
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,...
1
8550
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
8638
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
7381
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...
1
6191
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 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...
0
5662
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
4193
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
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.