Thread overview
Are templated functions always re-constructed?
Nov 16, 2021
rempas
Nov 16, 2021
H. S. Teoh
Nov 17, 2021
rempas
Nov 17, 2021
H. S. Teoh
Nov 19, 2021
rempas
Nov 17, 2021
Mike Parker
Nov 17, 2021
rempas
November 16, 2021

Let's say that I have the following function:

void add(T)(T val, T val2) { return val + val2; } // Classic example, lol

Now let's say that I call the function passing an int parameter. The function will get built with an int type. What happens the second time that I will call it again? Will it get the already build function or will it make a new one? I suppose it re-uses it but still I wanted to ask just in case. Please only answer if you are sure and not be making guesses because it is really important! Thanks!

November 16, 2021
On Tue, Nov 16, 2021 at 09:14:50PM +0000, rempas via Digitalmars-d-learn wrote:
> Let's say that I have the following function:
> 
> ```
> void add(T)(T val, T val2) { return val + val2; } // Classic example, lol
> ```
> 
> Now let's say that I call the function passing an `int` parameter. The function will get built with an `int` type. What happens the second time that I will call it again? Will it get the already build function or will it make a new one? I suppose it re-uses it but still I wanted to ask just in case. Please only answer if you are sure and not be making guesses because it is really important! Thanks!

Short answer: a template called with the same CT arguments will only be instantiated once, and reused thereafter.

Long answer: the above applies per compilation. If you compile your sources separately, there may be multiple instantiations of the template with the same CT arguments.  However, unless something went wrong, these duplicates will be dropped by the linker.


T

-- 
Making non-nullable pointers is just plugging one hole in a cheese grater. -- Walter Bright
November 17, 2021

On Tuesday, 16 November 2021 at 21:14:50 UTC, rempas wrote:

>

Let's say that I have the following function:

void add(T)(T val, T val2) { return val + val2; } // Classic example, lol

Now let's say that I call the function passing an int parameter. The function will get built with an int type. What happens the second time that I will call it again? Will it get the already build function or will it make a new one? I suppose it re-uses it but still I wanted to ask just in case. Please only answer if you are sure and not be making guesses because it is really important! Thanks!

You might find this useful:

https://dlang.org/blog/2020/07/31/the-abcs-of-templates-in-d/

November 17, 2021
On Tuesday, 16 November 2021 at 21:30:08 UTC, H. S. Teoh wrote:
>
> Short answer: a template called with the same CT arguments will only be instantiated once, and reused thereafter.
>
> Long answer: the above applies per compilation. If you compile your sources separately, there may be multiple instantiations of the template with the same CT arguments.  However, unless something went wrong, these duplicates will be dropped by the linker.
>
>
> T

Thanks! I mostly care about compilation times rather than duplication tbh
November 17, 2021

On Wednesday, 17 November 2021 at 02:50:43 UTC, Mike Parker wrote:

>

You might find this useful:

https://dlang.org/blog/2020/07/31/the-abcs-of-templates-in-d/

I do! Thanks a lot!

November 17, 2021
On Wed, Nov 17, 2021 at 11:12:22AM +0000, rempas via Digitalmars-d-learn wrote:
> On Tuesday, 16 November 2021 at 21:30:08 UTC, H. S. Teoh wrote:
> > 
> > Short answer: a template called with the same CT arguments will only be instantiated once, and reused thereafter.
> > 
> > Long answer: the above applies per compilation. If you compile your sources separately, there may be multiple instantiations of the template with the same CT arguments.  However, unless something went wrong, these duplicates will be dropped by the linker.
[...]
> Thanks! I mostly care about compilation times rather than duplication tbh

Regular (non-recursive) templates generally will not cause a noticeable change in compilation times.  The trouble usually only starts when you start using recursive templates.  Or when your templates are nested unreasonably deep (though this also usually only happens when there's recursive instantiation somewhere).


T

-- 
Computerese Irregular Verb Conjugation: I have preferences.  You have biases.  He/She has prejudices. -- Gene Wirchenko
November 19, 2021
On Wednesday, 17 November 2021 at 17:02:45 UTC, H. S. Teoh wrote:
>
> Regular (non-recursive) templates generally will not cause a noticeable change in compilation times.  The trouble usually only starts when you start using recursive templates.  Or when your templates are nested unreasonably deep (though this also usually only happens when there's recursive instantiation somewhere).
>
>
> T

That's awesome! Thanks!