how to inline member function is a separate file

S

Stanley Rice

If a member function is defined within the class body, the member function
is default inlined. However, if I want to define the function is a separate
file with other un-inlined function, Lippman said in C++ Primer that it is
ok. Just prefix the inline keyword in preceed the function definition.
However, It doesn't work for my compiler, gcc 4.4.3, and my compile option
is -Wall -Wextra -g -std=c++98

Thanks
 
I

Ian Collins

If a member function is defined within the class body, the member function
is default inlined. However, if I want to define the function is a separate
file with other un-inlined function, Lippman said in C++ Primer that it is
ok. Just prefix the inline keyword in preceed the function definition.
However, It doesn't work for my compiler, gcc 4.4.3, and my compile option
is -Wall -Wextra -g -std=c++98

If you are defining a member function outside of the class definition,
don't bother with inline, let the optimiser do its job. The only time
you will need (to prevent multiple definition link errors) it is if you
define the member function in a header file.
 
S

Stefan Ram

Stanley Rice said:
ok. Just prefix the inline keyword in preceed the function definition.

»inline« does not precede a function-definition, it is part
of a function-definition.

»An inline function shall be defined in every
translation unit in which it is used and shall have
exactly the same definition in every case (3.2).« 7.1.2p4

This means it needs to be defined in the ».h« or ».hpp« file.
 
J

Jun Fang

If a member function is defined within the class body, the member function
is default inlined. However, if I want to define the function is a separate
file with other un-inlined function, Lippman said in C++ Primer that it is
ok. Just prefix the inline keyword in preceed the function definition.
However, It doesn't work for my compiler, gcc 4.4.3, and my compile option
is -Wall -Wextra -g -std=c++98

Thanks

Keyword inline *suggest* compiler to inline.
Did you try -O option?
 
J

Juha Nieminen

Stefan Ram said:
»inline« does not precede a function-definition, it is part
of a function-definition.

»An inline function shall be defined in every
translation unit in which it is used and shall have
exactly the same definition in every case (3.2).« 7.1.2p4

This means it needs to be defined in the ».h« or ».hpp« file.

If it's a private member function that gets called only from the same
translation unit as the definition, than the 'inline' keyword does not
need to appear in the header file.
 
B

Bo Persson

Stefan said:
»inline« does not precede a function-definition, it is part
of a function-definition.

»An inline function shall be defined in every
translation unit in which it is used and shall have
exactly the same definition in every case (3.2).« 7.1.2p4

This means it needs to be defined in the ».h« or ».hpp« file.

No, it doesn't necessarily mean that. We first have to check the
compiler documentation for what it considers a translation unit.

Using g++ with link time optimizations (LTO) will compile the entire
program at once. I guess trying version 4.6 instead of 4.4 would make
it work.


Bo Persson
 
J

Jorgen Grahn

No, it doesn't necessarily mean that. We first have to check the
compiler documentation for what it considers a translation unit.

Using g++ with link time optimizations (LTO) will compile the entire
program at once. I guess trying version 4.6 instead of 4.4 would make
it work.

It seems strange and not very helpful to let a compiler option redefine
the concept of translation unit -- does gcc's LTO feature really do
that? E.g., will it suddenly fail to link code where there's a
'static int baz;' in foo.c and another in bar.c?

/Jorgen
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top