Template constraint on alias template parameter.

ag0aep6g anonymous at example.com
Thu Aug 6 18:09:50 UTC 2020


On Thursday, 6 August 2020 at 16:01:35 UTC, jmh530 wrote:
> The code below compiles, but I want to put an additional 
> constraint on the `test` function is only called with a Foo 
> struct.
>
> I tried things like is(T == Foo) and is(T : Foo), but those 
> don't work. However, something like is(T!int : Foo!int) works, 
> but is(T!U == Foo!U, U) doesn't. Any idea why is(T!U == Foo!U, 
> U) doesn't work?
>
> struct Foo(T)
> {
>     T x;
> }
>
> void test(alias T)()
>     if (__traits(isTemplate, T))
> {
>     import std.stdio: writeln;
>     writeln("there");
> }
>
> void main()
> {
>     test!Foo();
> }

`is(...)` only works on types. You're looking for 
`__traits(isSame, T, Foo)`.

For `is(T!U == Foo!U, U)` to work, the compiler would have to 
guess U. If the first guess doesn't work, it would have to guess 
again, and again, and again, until it finds a U that does work. 
Could take forever.


More information about the Digitalmars-d-learn mailing list