Function declarations and inline

M

MiG

Hello,

I would like to know if including the body of a function in its class
declaration results in the function being inlined.

In other words, is the following code...

template <typename T>
class foo
{
void foobar(T arg)
{
_data = arg;
}

.
:
.
};


.... equivalent to:

template <typename T>
class foo
{
inline void foobar(T arg);

.
:
.
};

template <typename T>
inline void foo<T>::foobar(T arg)
{
_data = arg;
}
 
V

Victor Bazarov

MiG said:
I would like to know if including the body of a function in its class
declaration results in the function being inlined.

Yes, it does. IOW, it's the same as supplying the function right
after the class definition (outside of it) with 'inline' modifier.

Now, you probably already know that 'inline' is not a directive to
the compiler, but rather a hint, which the compiler is free to
ignore.

V
 
M

MiG

Victor said:
MiG said:
I would like to know if including the body of a function in its class
declaration results in the function being inlined.

Yes, it does. IOW, it's the same as supplying the function right
after the class definition (outside of it) with 'inline' modifier.

Now, you probably already know that 'inline' is not a directive to
the compiler, but rather a hint, which the compiler is free to
ignore.

V

Thanks for that V, much appreciated!
 
M

MiG

Victor said:
MiG said:
I would like to know if including the body of a function in its class
declaration results in the function being inlined.

Yes, it does. IOW, it's the same as supplying the function right
after the class definition (outside of it) with 'inline' modifier.

Now, you probably already know that 'inline' is not a directive to
the compiler, but rather a hint, which the compiler is free to
ignore.

V

Victor,

I can't help but ask another question.

Are you against using compiler specific directives such as MsVC's
__forceinline, even when portability across platforms is not an issue?
I'm sure this won't be news for you but in case you don't know (you seem
to be a *nix user :) __forceinline works pretty much in the same way as
the standard inline and only differs in the compiler not performing the
cost/benefit analysis it does for inline functions/methods and a L1
warning being output whenever it can't/won't inline.
 
V

Victor Bazarov

MiG said:
Victor said:
MiG said:
I would like to know if including the body of a function in its
class declaration results in the function being inlined.

Yes, it does. IOW, it's the same as supplying the function right
after the class definition (outside of it) with 'inline' modifier.

Now, you probably already know that 'inline' is not a directive to
the compiler, but rather a hint, which the compiler is free to
ignore.

V

Victor,

I can't help but ask another question.

Are you against using compiler specific directives such as MsVC's
__forceinline, even when portability across platforms is not an issue?
I'm sure this won't be news for you but in case you don't know (you
seem to be a *nix user :)

For the record: I am not really a Unix user, although I can be if
needed. BTW, you could determine that by looking at the headers of
my posts. I am sure they will contain something incriminating like
"Outlook Express" or worse.
__forceinline works pretty much in the same
way as the standard inline and only differs in the compiler not
performing the cost/benefit analysis it does for inline
functions/methods and a L1 warning being output whenever it
can't/won't inline.

No, I am not against compiler-specific directives/pragmas/tricks when
portability is not an issue.

V
 
J

Juha Nieminen

Victor said:
Now, you probably already know that 'inline' is not a directive to
the compiler, but rather a hint, which the compiler is free to
ignore.

I thought 'inline' has completely lost its meaning of being a hint
to the compiler and has become exclusively a keyword for the linker.
It's like 'register' (except that 'register' doesn't mean anything
anymore, not even for the linker).
 
M

MiG

For the record: I am not really a Unix user, although I can be if
needed. BTW, you could determine that by looking at the headers of
my posts.

I completely missed that, as I keep forgetting that newsgroups are email
based (I'm new to the scene.) Thanks for reminding me.

I am sure they will contain something incriminating like
"Outlook Express" or worse.

It does indeed and I am shocked :D . Thought you'd be more the Moz
Thunderbird (well, OSS supporter) type of guy... Victor: Outlook?? :)

For the sake of your immaculate reputation I sincerely hope that the
case is you are being forced to use Outlook - and possibly worse, as you
pointed out - by some dictatorial entity that holds power over your
software preferences (employer/boss) and therefore is not your own decision.

Only kidding; I couldn't resist! :)

Thanks for your reply, V. As always it is much valued.
 
M

MiG

Juha said:
I thought 'inline' has completely lost its meaning of being a hint
to the compiler and has become exclusively a keyword for the linker.
It's like 'register' (except that 'register' doesn't mean anything
anymore, not even for the linker).

I don't think so. I don't know how it works in other platforms but in
MsVC you can enable generation of _compile-time_ assembler output, which
allows you to see the inline expansion of functions performed by the
compiler.

Link-time optimizations can also be enabled but these, AFAIK, affect
only cross-module inlining.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top