Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to inline member function is a separate file

Reply
Thread Tools

how to inline member function is a separate file

 
 
Stanley Rice
Guest
Posts: n/a
 
      02-20-2012
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-20-2012
On 02/20/12 06:09 PM, Stanley Rice wrote:
> 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.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      02-20-2012
Stanley Rice <(E-Mail Removed)> writes:
>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.

 
Reply With Quote
 
Jun Fang
Guest
Posts: n/a
 
      02-20-2012
On Feb 20, 1:09*pm, Stanley Rice <(E-Mail Removed)> wrote:
> 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?
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      02-20-2012
Stefan Ram <(E-Mail Removed)-berlin.de> wrote:
> Stanley Rice <(E-Mail Removed)> writes:
>>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.


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.
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      02-22-2012
Stefan Ram wrote:
> Stanley Rice <(E-Mail Removed)> writes:
>> 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.


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


 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      02-22-2012
On Wed, 2012-02-22, Bo Persson wrote:
> Stefan Ram wrote:
>> Stanley Rice <(E-Mail Removed)> writes:
>>> 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.

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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
inline class functions in separate file John Goche C++ 4 10-30-2006 09:18 PM
Use of inline function in inline function - is it allowed? TGOS C Programming 3 02-28-2005 10:49 AM
Function delcared inline but not defined inline Nish C Programming 4 10-08-2004 03:31 PM
How to use several separate classes (separate files) to be executed in one class (another file) EvgueniB Java 1 12-15-2003 01:18 AM



Advertisments