C++ An Inline Function Question

L

lucifer

Hi
I have a little problem regarding inline functions ,does declaring a
function inside the class definition make it inline or not.
 
E

Earl Purple

Hi
I have a little problem regarding inline functions ,does declaring a
function inside the class definition make it inline or not.

It makes it logically inline, in that you won't get multiple
definition link errors, but it's up to the compiler whether it is
actually physically inlined into the code.

If the function is virtual and coded inside the class definition it's
almost certain it won't be inlined in reality because at the point of
call it might not be this particular implementation that is going to
be invoked.
 
R

ralpe

Hi
I have a little problem regarding inline functions ,does declaring a
function inside the class definition make it inline or not.

It's the same as if you defined the function with the inline keyword
outside of the class. Whether this results in the function being
inlined is up to the compiler.
 
P

Pete Becker

Hi
I have a little problem regarding inline functions ,does declaring a
function inside the class definition make it inline or not.

The standard says: "A function defined within a class definition is an
inline function." [dcl.fct.spec]/3.
 
V

Vladislav.Lazarenko

Hi
I have a little problem regarding inline functions ,does declaring a
function inside the class definition make it inline or not.

It depends. C++ standard does not guarantee that function or method
will be inlined. In other words, "inline" keyword is just a suggestion
to your compiler. For example, some compilers may inline function or
method even without that keyword when optimizing your code.
 
D

D. Susman

Hi
I have a little problem regarding inline functions ,does declaring a
function inside the class definition make it inline or not.

Here is what parashift says:

When you declare an inline member function, it looks just like a
normal member function:

class Fred {
public:
void f(int i, char c);
};

But when you define an inline member function, you prepend the member
function's definition with the keyword inline, and you put the
definition into a header file:

inline
void Fred::f(int i, char c)
{
...
}

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.
 
P

Pete Becker

Here is what parashift says:

When you declare an inline member function, it looks just like a
normal member function:

That's one way to declare an inline member function, but it's not the
only way. As I mentioned earlier, defining the function inside the
class definition also makes it inline.
 
D

Dizzy

lucifer said:
Hi
I have a little problem regarding inline functions ,does declaring a
function inside the class definition make it inline or not.

Depends on what you mean by "makes it inline". If you mean if it's
equivalent to an inline defined function then yes. If you mean if that
actually makes the compiler inline the function (ie put its code at the
call place instead of actually calling it) then you don't know. AFAIK the
ISO C++ standard does not guarantee any inlining taking place in either
case (having the function definition in the class definition or as an
inline definition).

In practice, if using a modern compiler AND compiling with optimization AND
if the function is "small enough" then it should be inlined.
 
J

Juha Nieminen

It depends. C++ standard does not guarantee that function or method
will be inlined. In other words, "inline" keyword is just a suggestion
to your compiler. For example, some compilers may inline function or
method even without that keyword when optimizing your code.

'inline' has to do with linking, and yes, a function implemented in
the declaration of the class is inline.
 

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

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,650
Latest member
IanTylor5

Latest Threads

Top