POV-Ray : Newsgroups : povray.tools.general : tga->df3 : Re: tga->df3 Server Time
17 May 2024 04:43:34 EDT (-0400)
  Re: tga->df3  
From: Warp
Date: 5 Oct 2004 08:23:16
Message: <41629234@news.povray.org>
Ross <rli### [at] everestkcnet> wrote:
> besides being required in order to have the compiler consider inlining it?

  Quite ironically, most compilers completely ignore the 'inline' keyword
when they evaluate whether a function is worth inlining or not. That is,
the 'inline' keyword has usually no effect whatsoever on the probability
of a function being inlined.
  In this context 'inline' is exactly as obsolete as eg. 'register' (which
compilers completely ignore).

  However, unlike 'register', 'inline' is an essential keyword, but for a
rather different reason than inlining.

  When you declare a function 'inline', you are telling the compiler
"if the implementation of this function appears in more than one object
file, the linker must merge them to one".

  Normally if the implementation of a certain function appears in two
different object files being linked to the same binary, the linker will
issue an error message: The linker can't know which one of them to use.
  However, if you had declared the functions 'inline', the compiler will
instruct the linker that both implementations are actually the same and
the linker will then use one of them.

  This is relevant when the implementation of a function is in a header
file: If the header file is included in more than one source file, this
implementation will be then placed in more than one object file.

  In C this problem was solved by defining the function 'static', which
made the function local to that object file (the same thing as is in C++
better done with a nameless namespace). However, using 'static' is not
the same as using 'inline'.
  For one, if you use 'static', the function code will be duplicated in
the final binary (increasing its size), but more importantly, functions
like this would not work properly:

static int foo()
{
    static int counter = 0;
    return ++counter;
}

  If this resides in a header file which is included in several places,
each place will get their own 'foo()' with its own counter, which might
not be what one wants.
  If, however, you do it like this (in C++):

inline int foo()
{
    static int counter = 0;
    return ++counter;
}

  Every object file calling 'foo()' will be calling one unique 'foo()',
which modifies one unique 'counter' and thus it will work as expected.

  This is why 'inline' is important.

  By the way, it's good to know that:

  - Member functions are implicitly inline if they are implemented where
    they are declared.
  - Template functions are implicitly inline.

-- 
plane{-x+y,-1pigment{bozo color_map{[0rgb x][1rgb x+y]}turbulence 1}}
sphere{0,2pigment{rgbt 1}interior{media{emission 1density{spherical
density_map{[0rgb 0][.5rgb<1,.5>][1rgb 1]}turbulence.9}}}scale
<1,1,3>hollow}text{ttf"timrom""Warp".1,0translate<-1,-.1,2>}//  - Warp -


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.