Does `is` expression with template alias need fixing.
FeepingCreature
feepingcreature at gmail.com
Wed Mar 22 12:43:43 UTC 2023
On Wednesday, 22 March 2023 at 12:34:26 UTC, FeepingCreature
wrote:
> 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);
> }
> ```
Looks like it substitutes through simple cases but fails with
more complex ones?
```
#include <type_traits>
template <typename T, int M, int N>
struct Matrix {
};
template <typename T>
using Vector3 = typename std::conditional<true, Matrix<T, 3, 1>,
void>::type;
template<typename T>
void baz(Vector3<T>& vector)
{
}
int main() {
auto v = Vector3<int>();
/*
test.cpp:17:6: error: no matching function for call to
‘baz(Matrix<int, 3, 1>&)’
17 | baz(v);
| ~~~^~~
test.cpp:11:6: note: candidate: ‘template<class T> void
baz(Vector3<T>&)’
11 | void baz(Vector3<T>& vector)
| ^~~
test.cpp:11:6: note: template argument deduction/substitution
failed:
test.cpp:17:6: note: couldn’t deduce template parameter ‘T’
*/
baz(v);
}
```
That much we can probably implement in D, granted: specifically
recognize trivial aliases in the template resolution code.
Do file a bug for that.
More information about the Digitalmars-d
mailing list