Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
September 02, 2010 I don't understand a const | ||||
---|---|---|---|---|
| ||||
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 |
September 02, 2010 Re: I don't understand a const | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile <bearophileHUGS@lycos.com> 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 Simpler test case: void bar(T)(const T x, out T y) {} void main() { const int s1; int s2; bar(s1, s2); } It seems DMD is confused by const(int) being such a nice fit for the first parameter. This might have to do with s2 = s1 being ok. It is probably worth noting that this works: import std.traits; void bar(T)(const T x, out T y) {} void main() { const int s1; int s2; bar!(CommonType!(s1,s2))(s1, s2); } -- Simen |
September 02, 2010 Re: I don't understand a const | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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).
|
September 02, 2010 Re: I don't understand a const | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | On 9/2/2010 5:59 AM, Simen kjaeraas wrote: > bearophile <bearophileHUGS@lycos.com> 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 > > Simpler test case: > > void bar(T)(const T x, out T y) {} > > void main() { > const int s1; > int s2; > bar(s1, s2); > } > > It seems DMD is confused by const(int) being such a nice fit for the first parameter. This might have to do with s2 = s1 being ok. > > It is probably worth noting that this works: > > > import std.traits; > > void bar(T)(const T x, out T y) {} > > void main() { > const int s1; > int s2; > bar!(CommonType!(s1,s2))(s1, s2); > } http://d.puremagic.com/issues/show_bug.cgi?id=4594 |
September 02, 2010 Re: I don't understand a const | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Roberts | On Thu, 02 Sep 2010 11:11:52 -0700, Brad Roberts wrote:
> On 9/2/2010 5:59 AM, Simen kjaeraas wrote:
>> void bar(T)(const T x, out T y) {}
>>
>> void main() {
>> const int s1;
>> int s2;
>> bar(s1, s2);
>> }
>>
>> It seems DMD is confused by const(int) being such a nice fit for the first parameter. This might have to do with s2 = s1 being ok.
>>
>> It is probably worth noting that this works:
>>
>>
>> import std.traits;
>>
>> void bar(T)(const T x, out T y) {}
>>
>> void main() {
>> const int s1;
>> int s2;
>> bar!(CommonType!(s1,s2))(s1, s2);
>> }
>
> http://d.puremagic.com/issues/show_bug.cgi?id=4594
Right! its dmd bug in implicit instantiation:
void bar(T)(const T x, T y) {}
void main () {
const int s1;
int s2;
bar!(const int)(s1, s2); // pass ok
bar!(int)(s1, s2); // pass ok
bar(s1, s2); // error
}
|
September 02, 2010 Re: I don't understand a const | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Roberts | Brad Roberts:
> http://d.puremagic.com/issues/show_bug.cgi?id=4594
Thanks for all the answers.
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation