inline functions in a c++ class

R

Rahul

Hi Everyone,

I had this doubt for quite some while, does defining a member
function within the class declaration makes it inline by default?
Would the compiler attempt to inline all calls to such member
functions?

class sample
{
public: void print()
{
cout<<"sample::print function invoked";
}
};

int main()
{
sample obj;
obj.print();
return(0);
}

Thanks in advance!!!
 
R

Rolf Magnus

Rahul said:
Hi Everyone,

I had this doubt for quite some while, does defining a member
function within the class declaration makes it inline by default?

Yes, if you mean that it has the same efffect as using the inline keyword on
the function.
Would the compiler attempt to inline all calls to such member
functions?

It might do that, or not. The only guaranteed effect of the inline keyword
in C++ is that there can be multiple definitions of the function in
different translation units, even if that function has external linkage.
Regarding optimization, the inline keyword is just a hint to the compilier
that some compilers simply ignore, deciding on their own when to inline a
function call and when not to.
 
R

Rahul

It might do that, or not. The only guaranteed effect of the inline keyword
in C++ is that there can be multiple definitions of the function in
different translation units, even if that function has external linkage.

I didn't know that... do you have any links for further info on this
one?
 
A

Andrey Tarasevich

Rahul said:
I had this doubt for quite some while, does defining a member
function within the class declaration makes it inline by default?

Yes, it does.
Would the compiler attempt to inline all calls to such member
functions?

It depends on many factors and in reality virtually all of them are
implementation-dependent. It might inline all calls, some of the calls,
or none of the calls. The decision to inline can really be made on
per-call basis.

Moreover, the compiler might inline calls to the functions that are not
explicitly declared 'inline' at its own discretion. For this reason, the
relationship between the 'inline' specifier and the actual inlining of
the calls might be (and is) significantly more loose then it is often
assumed to be. As other already noted, the only thing the 'inline'
specifier is guaranteed to do is to allow multiple definitions of the
same function with external linkage in the program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top