POV-Ray : Newsgroups : povray.off-topic : My first C++ program : Re: A test Server Time
30 Sep 2024 21:32:42 EDT (-0400)
  Re: A test  
From: Warp
Date: 24 Sep 2008 09:17:09
Message: <48da3dd5@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> The C++ tutorial I'm reading has a section on how to inline functions. 
> Now *clearly* the compiler cannot inline something that it can't *find*. 
> So all that stuff about putting code into header files if you want it 
> inlined across compilation units is a necessary part of the way a C++ 
> compiler works. But does C++ ever inline functions within *the same* 
> compilation unit automatically? Or does it literally refuse to inline 
> every function that doesn't say "inline" on it?

  Whether the compiler will inline a function (from which it has the
implementation at that point) is unspecified and completely up to the
compiler. The 'inline' keyword doesn't really matter in this. The
compiler may well inline a function which does not have the 'inline'
keyword, and it may well not inline a function which has it. In a way,
the 'inline' keyword is a no-op, at least with respect to actual inlining.

  It's not a no-op keyword overall, because it controls how the linker
behaves with that function. If the linker encounters the same 'inline'
function in more than one object file, it will use only one of them and
make all the code which calls that function to use that one.

  If you put a function implementation in a header file, you must make
it 'inline' to avoid linker errors (if the header file is used in more
than one compilation unit). If you don't make it 'inline', you are
effectively instantiating the same function in more than one compilation
unit, and the linker considers this to be an error (name collision of
two different functions).

  (Btw, template functions are implicitly 'inline', so the keyword doesn't
have to be used with them.)

-- 
                                                          - Warp


Post a reply to this message

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