Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14305 > unrolled thread

how to inline member function is a separate file

Started byStanley Rice <heconghui@gmail.com>
First post2012-02-19 21:09 -0800
Last post2012-02-22 21:42 -0600
Articles 7 — 7 participants

Back to article view | Back to comp.lang.c++


Contents

  how to inline member function is a separate file Stanley Rice <heconghui@gmail.com> - 2012-02-19 21:09 -0800
    Re: how to inline member function is a separate file Ian Collins <ian-news@hotmail.com> - 2012-02-20 18:31 +1300
    Re: how to inline member function is a separate file Jun Fang <heyfng@gmail.com> - 2012-02-20 02:16 -0800
    Re: how to inline member function is a separate file Juha Nieminen <nospam@thanks.invalid> - 2012-02-20 10:26 +0000
    Re: how to inline member function is a separate file "Bo Persson" <bop@gmb.dk> - 2012-02-22 20:16 +0100
      Re: how to inline member function is a separate file Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-22 20:29 +0000
        Re: how to inline member function is a separate file Robert Wessel <robertwessel2@yahoo.com> - 2012-02-22 21:42 -0600

#14305 — how to inline member function is a separate file

FromStanley Rice <heconghui@gmail.com>
Date2012-02-19 21:09 -0800
Subjecthow to inline member function is a separate file
Message-ID<17823047.699.1329714548355.JavaMail.geo-discussion-forums@pbgq3>
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

[toc] | [next] | [standalone]


#14306

FromIan Collins <ian-news@hotmail.com>
Date2012-02-20 18:31 +1300
Message-ID<9qe456FfoaU1@mid.individual.net>
In reply to#14305
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

[toc] | [prev] | [next] | [standalone]


#14312

FromJun Fang <heyfng@gmail.com>
Date2012-02-20 02:16 -0800
Message-ID<839203f2-535b-415d-aff7-6caedd44df66@p13g2000yqd.googlegroups.com>
In reply to#14305
On Feb 20, 1:09 pm, Stanley Rice <hecong...@gmail.com> 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?

[toc] | [prev] | [next] | [standalone]


#14314

FromJuha Nieminen <nospam@thanks.invalid>
Date2012-02-20 10:26 +0000
Message-ID<4f421fe5$0$2815$7b1e8fa0@news.nbl.fi>
In reply to#14305
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> Stanley Rice <heconghui@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#14431

From"Bo Persson" <bop@gmb.dk>
Date2012-02-22 20:16 +0100
Message-ID<9qkt89FdajU1@mid.individual.net>
In reply to#14305
Stefan Ram wrote:
> Stanley Rice <heconghui@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#14433

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2012-02-22 20:29 +0000
Message-ID<slrnjkak0e.1ls.grahn+nntp@frailea.sa.invalid>
In reply to#14431
On Wed, 2012-02-22, Bo Persson wrote:
> Stefan Ram wrote:
>> Stanley Rice <heconghui@gmail.com> 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   .

[toc] | [prev] | [next] | [standalone]


#14442

FromRobert Wessel <robertwessel2@yahoo.com>
Date2012-02-22 21:42 -0600
Message-ID<m1dbk7981v9qfqkudvi43g58d03o2lsffs@4ax.com>
In reply to#14433
On 22 Feb 2012 20:29:05 GMT, Jorgen Grahn <grahn+nntp@snipabacken.se>
wrote:

>On Wed, 2012-02-22, Bo Persson wrote:
>> Stefan Ram wrote:
>>> Stanley Rice <heconghui@gmail.com> 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?


No, the compiler is still required to keep the various name spaces
straight.

LTCG does not redefine the concept of a translation unit - a
translation unit is conceptual anyway, and the compile process is only
required to have an end result as-if the translation unit was
processed in the conceptual manner.

LTCG is available on many compilers (GCC is something of a latecomer),
and often produces useful optimizations across traditional translation
unit boundaries.  It's also pretty commonly used.  If you use MS's
product, LTCG is now the default for "release" builds.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web