Template specialisation, "Generic type locking", offline stdlib docs and case-based template question

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Dec 1 04:40:59 UTC 2017


On Friday, December 01, 2017 03:39:12 helxi via Digitalmars-d-learn wrote:
> 1. Template specialisation.
> Why is this useful?:
> T getResponse(T = int)(string question); And how does it differ
> from
> int getResponse(string question); ?
>
> Context: http://ddili.org/ders/d.en/templates.html : Section:
> "Default template parameters"

I would point out that that isn't template specialization at all. Template
specialization uses : not =, and would look like

T getResponse(T : int)(string question) {...}

and is a different beast entirely. As the section you're referring to
mentions, what you have there is a default template parameter, which is
useful in cases where you want to be able to have the flexibility of a
template parameter but not require it in the common case. One prime example
is std.algorithm's find function. You can pass it a predicate that it will
use for comparing elements in the range, but by default it uses equality.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list