CTFE & template predicates
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon May 4 05:00:18 PDT 2015
On Monday, 4 May 2015 at 11:22:16 UTC, Robert M. Münch wrote:
> Hi, ok, just to better understand this (I have a C++ background
> (even quite old)): When I want to use some functions I need to
> specify the type? It's not possible to use T.length() which
> would compile if T is a string? I thought that it's just
> generic and I can throw in T.
In template parameter lists, a solitary identifier is a type
parameter. You can only pass types in those.
In function (pointer) declarations, a solitary identifier is the
type of the parameter.
In function/delegate literals, a solitary identifier is the name
of the parameter and the type is inferred. I guess this is what
you were thinking of.
----
void main()
{
template t(T) {} /* T is a template type parameter. */
alias i = t!int; /* It can be instantiated with the type int,
for example, */
static assert(!__traits(compiles, t!123)); /* but not with a
value. */
void function(int) fp; /* int is the type of the parameter. */
fp = (x) {}; /* x is the name of the parameter whose type is
inferred from above. */
}
----
More information about the Digitalmars-d-learn
mailing list