how to inline member function is a separate file

Discussion in 'C++' started by Stanley Rice, Feb 20, 2012.

  1. Stanley Rice

    Stanley Rice Guest

    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
    Stanley Rice, Feb 20, 2012
    #1
    1. Advertising

  2. Stanley Rice

    Ian Collins Guest

    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
    Ian Collins, Feb 20, 2012
    #2
    1. Advertising

  3. Stanley Rice

    Stefan Ram Guest

    Stanley Rice <> 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.
    Stefan Ram, Feb 20, 2012
    #3
  4. Stanley Rice

    Jun Fang Guest

    On Feb 20, 1: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
    >
    > Thanks


    Keyword inline *suggest* compiler to inline.
    Did you try -O option?
    Jun Fang, Feb 20, 2012
    #4
  5. Stefan Ram <-berlin.de> wrote:
    > Stanley Rice <> 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.
    Juha Nieminen, Feb 20, 2012
    #5
  6. Stanley Rice

    Bo Persson Guest

    Stefan Ram wrote:
    > Stanley Rice <> 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
    Bo Persson, Feb 22, 2012
    #6
  7. Stanley Rice

    Jorgen Grahn Guest

    On Wed, 2012-02-22, Bo Persson wrote:
    > Stefan Ram wrote:
    >> Stanley Rice <> 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 .
    Jorgen Grahn, Feb 22, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EvgueniB
    Replies:
    1
    Views:
    623
    Anthony Borla
    Dec 15, 2003
  2. Frank Fredstone
    Replies:
    1
    Views:
    437
    Jean-Francois Briere
    Jun 27, 2006
  3. Nish
    Replies:
    4
    Views:
    487
    Thomas Stegen
    Oct 8, 2004
  4. TGOS
    Replies:
    3
    Views:
    378
    Kevin Bracey
    Feb 28, 2005
  5. John Goche
    Replies:
    4
    Views:
    344
    mlimber
    Oct 30, 2006
Loading...

Share This Page