Topic: Inline keyword: when required?


Author: David R Tribble <david.tribble@noSPAM.central.beasys.com>
Date: 1998/06/11
Raw View
Charles Miller wrote:
> class X
> {
>    X() {}
>    void f();
>    inline void g();
>    inline void h();
> };
>
> inline void X::f() {}
>            void X::g() {}
> inline void X::h() {}
>
> The above compiles with Borland without complaint.  But, is there a
> rule for when to use "inline" in the function definition?  MUST it
> appear in the class definition, or the function definition, or both?

I can't quote from the Draft, but I would seem logical to me that
'inline' is required on either the function declaration, or
definition, or both, provided that the compiler sees both the
declaration and the definition in the same compilation unit.

Obviously, it's safe to both declare and define them as inline in
the same header.  This doesn't give the compiler any conflicting
information:

    // x.h
    class X
    {
        inline void g();
    };

    inline void X::g() { ... }      // Okay

    // y.cpp
    #include "x.h"
    void foo()
    {
        X::g();    // Will attempt to inline X::g()
    }

If you provide inline func definitions in the same header file as the
class declaration, then you only need 'inline' on the func definitions.
The compiler figures that you prefer the func to be implemented inline:

    // x.h
    class X
    {
        void g();
    };

    inline void X::g() { ... }      // Okay

    // y.cpp
    #include "x.h"
    void foo()
    {
        X::g();    // Will attempt to inline X::g()
    }

But if you don't supply inline func definitions in the header file,
but still declare the funcs as inline in the class declaration, then
presumably you intend to supply the inline func definitions in the
source file itself.  If you don't, you've effectively told the
compiler that you want the funcs to be inline, but haven't told it
how to make them inline.  I'm not sure what happens in this case;
it's possible that 1) the compiler complains, or 2) the compiler
assumes you will supply non-inline versions of the funcs (in some
other object file) at link time.  (Presumably Borland does (2).)
Like this:

    // x.h
    class X
    {
        inline void g();
    };

    // y.cpp
    #include "x.h"
    void foo()
    {
        X::g();    // Assumes non-inline X::g() exists?
    }

    // x.cpp
    #include "x.h"
    void X::g() { ... }      // Okay? Or will compiler complain?

-- David R. Tribble, david.tribble@central.beasys.com --


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/06/05
Raw View
"Charles Miller" <cmiller@oasystel.com> writes:

>class X
>{
>   X() {}
>   void f();
>   inline void g();
>   inline void h();
>};

>inline void X::f() {}
>           void X::g() {}
>inline void X::h() {}


>The above compiles with Borland without complaint.  But, is there a rule for
>when to use "inline" in the function definition?  MUST it appear in the
>class definition, or the function definition, or both?

Ordinarily there is no need to put "inline" on the declaration
of a member function when it is defined outside the class. You can
do so if you wish.

Whether or not it is declared inline in the class, you can declare
it inline on the separate function definition.  If a member function
is declared inline in either place (declaration or definition) it is
an inline function.

Reference: Draft standard 9.3 "Member functions", paragraph 3.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Charles Miller" <cmiller@oasystel.com>
Date: 1998/06/04
Raw View
Example:

class X
{
   X() {}
   void f();
   inline void g();
   inline void h();
};

inline void X::f() {}
           void X::g() {}
inline void X::h() {}


The above compiles with Borland without complaint.  But, is there a rule for
when to use "inline" in the function definition?  MUST it appear in the
class definition, or the function definition, or both?

Charles Miller
cmiller@oasystel.com

www.oasystel.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]