Function templates do implicit conversions for their arguments
Maxim Fomin
maxim at maxim-fomin.ru
Thu Jul 4 06:45:06 PDT 2013
On Thursday, 4 July 2013 at 09:24:53 UTC, TommiT wrote:
>
> Yes, you are right in that the quote from TDPL refers to that
> specific function signature and is saying that the base type of
> T[] and T must match. But more generally, that quote is also
> saying that D doesn't try to do implicit conversion and type
> deduction at the same time for type-parameterized runtime
> arguments. In other words, the quote is saying: the type of the
> argument you pass to a templated function must match exactly to
> the templated type that the function template is expecting for
> that particular argument. That is: no implicit conversion
> happens to your variable before it is passed to the function as
> a runtime argument whose type is parameterized by the function.
>
>
You heavily misunderstoond the topic. The issue here is that in
T[] find(T)(T[] haystack, T needle)
type of neendle should correspond to base type of haystack. If
there is a contradiction ("at the same time implicit conversions
and type deduction"), dmd issues error. But here
void foo(T)(T[] slice) { }
base type of slice is not tied to any other parameters. Actually
if you pass integer static array, dmd deduces T to be int, then
template instantiation part is done. After that static array is
casted to slice as what happens with non-template function
(internally dmd treats it like foo(cast(int[])arr)). Other your
comments are based on this misunderstanding.
More information about the Digitalmars-d
mailing list