|
![](/i/fill.gif) |
Le 20/04/2012 03:16, Darren New a écrit :
> On 4/17/2012 8:06, Warp wrote:
>> Invisible<voi### [at] dev null> 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
|
![](/i/fill.gif) |