digitalmars.D.learn - Are templated functions always re-constructed?
- rempas (12/12) Nov 16 2021 Let's say that I have the following function:
- H. S. Teoh (10/22) Nov 16 2021 Short answer: a template called with the same CT arguments will only be
- rempas (3/11) Nov 17 2021 Thanks! I mostly care about compilation times rather than
- H. S. Teoh (10/21) Nov 17 2021 Regular (non-recursive) templates generally will not cause a noticeable
- rempas (2/9) Nov 19 2021 That's awesome! Thanks!
- Mike Parker (3/15) Nov 16 2021 You might find this useful:
- rempas (2/4) Nov 17 2021 I do! Thanks a lot!
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!
Nov 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
Nov 16 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. TThanks! I mostly care about compilation times rather than duplication tbh
Nov 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 tbhRegular (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
Nov 17 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). TThat's awesome! Thanks!
Nov 19 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/
Nov 16 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!
Nov 17 2021