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

helxi brucewayneshit at gmail.com
Fri Dec 1 03:39:12 UTC 2017


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"

2. "Generic locking".
Is it possible to specialise templates for a certain group of 
types? For example
auto fn(T)(T arg)
auto fn(T : int, double, float, ulong)(T arg); //shares same 
behaviour
auto fn(T : char, string, dchar, wchar, dstring, wstring)(T arg); 
// shares same behavior

3. "Offline docs".
Is there any offline documentation of the stdlib? Like 
https://en.cppreference.com/mwiki/index.php?title=Cppreference:Archives&oldid=95461

4. Case based question regarding templates:

class Stack(T)
{
private:
     T[] data;
public:
     this(T)(T[] data){ /*..*/}
     this(T)(){}

    //...

}

void main()
{
     auto s = new Stack!int;
}

Says:
  Error: template app.Stack!int.Stack.__ctor cannot deduce 
function from argument types !()(), candidates are:
source/app.d(6,2):        app.Stack!int.Stack.__ctor(T)(T[] data)
source/app.d(9,2):        app.Stack!int.Stack.__ctor(T)()

Why is Stack!int a ctor()() instead of a ctor(int)()?


More information about the Digitalmars-d-learn mailing list