Does `is` expression with template alias need fixing.

FeepingCreature feepingcreature at gmail.com
Wed Mar 22 12:34:26 UTC 2023


On Wednesday, 22 March 2023 at 12:30:46 UTC, FeepingCreature 
wrote:
> On Wednesday, 22 March 2023 at 10:43:15 UTC, Elfstone wrote:
>> I don't know how C++ compilers resolve `template using` just 
>> fine.
>>
>> ```C++
>> template <typename T>
>> using Vector3 = Matrix<T, 3, 1>;
>> ```
>>
>> I declare an alias and I use it everywhere. It acts as a 
>> natural constraint. I never needed `isVector3` with my old C++ 
>> code. I expected D could do the same, and was really 
>> frustrated when I found out it couldn't. Even more frustrated 
>> when I read Steven's reply, that the bug with `is` has been 
>> there for 16 years.
>>
>> When the compiler allows people to use template aliases as 
>> template function parameters, it should make it work, or 
>> reject it aloud. Maybe some future system can save the day, 
>> but does it mean the old `alias` will just be left bugged?
>
> You can perfectly fine use `alias Vector3(T) = Matrix!(T, 3, 
> 1)` in D. You cannot *constrain a type to be Vector3* in D, but 
> you cannot in C++ either. (I don't think so? I'm poking at 
> C++20 concepts, but I don't see a way to do it.)

Oh wait, correction: you *can* do it! Neat! I wonder how that 
works.

Probably it's because `using` is more syntactically constrained?

```
template <typename T, int M, int N>
struct Matrix {
};

template <typename T>
using Vector3 = Matrix<T, 3, 1>;

template<typename T>
void baz(Vector3<T>& vector)
{
}

int main() {
   auto v = Vector3<int>();
   baz(v);
}
```


More information about the Digitalmars-d mailing list