POV-Ray : Newsgroups : povray.off-topic : Teach yourself C++ in 21 days : Re: Days 5- Server Time
30 Jul 2024 00:19:28 EDT (-0400)
  Re: Days 5-  
From: Le Forgeron
Date: 20 Apr 2012 03:15:39
Message: <4f910d1b$1@news.povray.org>
Le 20/04/2012 03:16, Darren New a écrit :
> On 4/17/2012 8:06, Warp wrote:
>> Invisible<voi### [at] devnull>  wrote:
>>>>     Likewise template functions are implicitly 'inline' without
>>>> having to
>>>> explicitly say so.
>>
>>> Well, by definition a template generates a new copy of the function each
>>> time you use it, no?
>>
>>    No. A new version is created for each used *type*.
> 
> Technically, I think a new copy of the function is generated/compiled
> for each compilation unit that uses it for each type, but the different
> versions for the same type are merged, right?
> 
> I.e., if x.c uses myfunc<int>() and y.c uses myfunc<int>() you'd need to
> compile that function twice and toss one at link time, right?
> 

IIRC, If the call to myfunc<int>() is done in the same compilation unit
(same top level cpp file), it will be generated once as "static
function" (jump to local symbol, at best; that does not work if it
become inlined code of course).

If they are in separate compilation units (different top level *.cpp
files), each units will get its own version.

Linker will not remove either. (because it has no way to know if the one
in unit A is identical to the one in unit B: naming is no use, you might
have compiled unit A with the version of Monday, and unit B with the
version of Saturday.
(that's the issue with template: generated code can grow very fast if
you use the template in many *.cpp files)


Post a reply to this message

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