digitalmars.D.learn - I don't understand a const
- bearophile (15/15) Sep 01 2010 This program doesn't compile, dmd prints the errors:
- Simen kjaeraas (20/37) Sep 02 2010 Simpler test case:
- Brad Roberts (2/54) Sep 02 2010 http://d.puremagic.com/issues/show_bug.cgi?id=4594
- Michal Minich (10/36) Sep 02 2010 Right! its dmd bug in implicit instantiation:
- bearophile (4/5) Sep 02 2010 Thanks for all the answers.
- Michal Minich (12/35) Sep 02 2010 When foo is defined this way, the original example works.
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
Sep 01 2010
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, bearophileSimpler 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
Sep 02 2010
On 9/2/2010 5:59 AM, Simen kjaeraas wrote:bearophile <bearophileHUGS lycos.com> wrote:http://d.puremagic.com/issues/show_bug.cgi?id=4594This 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, bearophileSimpler 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); }
Sep 02 2010
On Thu, 02 Sep 2010 11:11:52 -0700, Brad Roberts wrote:On 9/2/2010 5:59 AM, Simen kjaeraas wrote: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 }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
Sep 02 2010
Brad Roberts:http://d.puremagic.com/issues/show_bug.cgi?id=4594Thanks for all the answers. Bye, bearophile
Sep 02 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, bearophileWhen 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).
Sep 02 2010