Fix template parameter
Paul Backus
snarwin at gmail.com
Tue Aug 9 21:16:22 UTC 2022
On Tuesday, 9 August 2022 at 21:08:52 UTC, Meta wrote:
> (it may also include anything that is a subtype of BigInt...
> I've received different answers on what exactly `(T: SomeType)`
> means in this context).
Yes, this syntax allows anything that implicitly converts to
`BigInt`; for example:
```d
import std.bigint;
void fun(T : BigInt)(T t)
{
pragma(msg, "Instantiated with T = `" ~ T.stringof ~ "`");
}
struct S
{
BigInt n;
alias n this;
}
void main()
{
S s;
fun(s); // Instantiated with T = `S`
}
```
There is currently no way to write a template specialization that
requires an exact type, with no implicit conversions. For that,
you would have to use a template constraint instead:
```d
void fun(T)(T t)
if (is(T == BigInt))
{
// ...
}
```
More information about the Digitalmars-d-learn
mailing list