Template type inference problem
Manu
turkeyman at gmail.com
Wed Oct 2 23:34:21 UTC 2024
On Wed, 2 Oct 2024 at 20:06, kdevel via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:
> On Wednesday, 2 October 2024 at 08:55:15 UTC, Manu wrote:
> > Does anyone understand why this doesn't work?
> >
> > void f(T)(const(T)[] x, const(T)* y) {}
> > void test()
> > {
> > int*[] x;
> > const int* y;
> > f(x, &y);
> > }
> >
> > error : template `f` is not callable using argument types
> > `!()(int*[],
> > const(int*)*)`
> > Candidate is: `f(T)(const(T)[] x, const(T)* y)`
> >
> > Should this work? It looks like it should work to me.
> > ...assuming T is inferred to be `int*`... which it's not clear
> > why it
> > wouldn't be?
> >
> > The argument `y` is an exact match.
>
> The second actual parameter to f!int is &y not y.
>
Yes, the `y` argument is `const(int*)*`, so `T` must be inferred `int*`
> The argument `x` requires a const promotion,
>
> The type of x is int*[] and not const(int) [];
Right, `x` is `int*[]`, and matching the argument `const(T)[]` required
const promotion `int*[]` -> `const(int*)[]`, which is a perfectly normal
promotion. In which case, `T` is `int*`, matching with `y`, and so `T` can
be properly inferred.
If you remove the `*` from `x` and `y`, the `T` inference works properly...
so something about `T` being inferred as `int*` rather than an `int` causes
the type inference to fail.
The non-uniformity looks like a bug, unless there's a reasonable
explanation that I've missed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20241003/9ca5d396/attachment.htm>
More information about the Digitalmars-d
mailing list