I don't understand a const
Michal Minich
michal.minich at gmail.com
Thu Sep 2 09:20:31 PDT 2010
On Wed, 01 Sep 2010 22:22:22 -0400, bearophile wrote:
> This program doesn't compile, dmd prints the errors: test.d(4): Error:
> template test.foo(T) does not match any function template declaration
> test.d(4): Error: template test.foo(T) cannot deduce template function
> from argument types !()(const(int),int) test.d(9): Error: template
> instance test.bar!(int) error instantiating
>
> Are you able to tell me what is the error in this code?
>
>
> void foo(T)(const T a, out T b) {}
>
> void bar(T)(const T x, out T y) {
> foo(x, y); // line 4
> }
>
> void main() {
> int s1, s2;
> bar(s1, s2); // line 9
> }
>
>
> Bye, and thank you,
> bearophile
When foo is defined this way, the original example works.
void foo(T, U)(const T a, out U b) {}
foo must use 2 type parameters, because it is called with two types from
line 4 x : const(int) and y : int. It is ok to call bar (s1, s2) because
both variables are int at the call site. the s1 is implicitly converted
to const after the template function is called.
The important thing here is - compiler does not try to implicitly convert
type when matching template instantiation.
Therefore it does not try to convert second argument y to const(int)
(which would result in template match, which in this example does even
does not make sense - const out).
More information about the Digitalmars-d-learn
mailing list