www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Confused by refusal to expand template

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
Code:
	struct S {
		int f(K)(K x) {
			return 1;
		}
		void func(K)(inout(K) x) {
			auto h = f(x);
		}
	}
	void main() {
		S s;
		s.func("abc");	// This is line 44
	}

This refuses to compile:

test2.d(44): Error: template test2.S.func(K) does not match any function
template declaration
test2.d(44): Error: template test2.S.func(K) cannot deduce template function
from argument types !()(string)

Removing 'inout' fixes the problem. But I don't understand why.


T

-- 
Perhaps the most widespread illusion is that if we were in power we would
behave very differently from those who now hold it---when, in truth, in order
to get power we would have to become very much like them. -- Unknown
Mar 23 2012
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 03/23/2012 11:52 PM, H. S. Teoh wrote:
 Code:
 	struct S {
 		int f(K)(K x) {
 			return 1;
 		}
 		void func(K)(inout(K) x) {
 			auto h = f(x);
 		}
 	}
 	void main() {
 		S s;
 		s.func("abc");	// This is line 44
 	}

 This refuses to compile:

 test2.d(44): Error: template test2.S.func(K) does not match any function
template declaration
 test2.d(44): Error: template test2.S.func(K) cannot deduce template function
from argument types !()(string)

 Removing 'inout' fixes the problem. But I don't understand why.


 T

You already brought this up IIRC. The IFTI matching algorithm is too weak. I think it should work.
Mar 23 2012
prev sibling next sibling parent reply David <d dav1d.de> writes:
Am 23.03.2012 23:52, schrieb H. S. Teoh:
 Code:
 	struct S {
 		int f(K)(K x) {
 			return 1;
 		}
 		void func(K)(inout(K) x) {
 			auto h = f(x);
 		}
 	}
 	void main() {
 		S s;
 		s.func("abc");	// This is line 44
 	}

 This refuses to compile:

 test2.d(44): Error: template test2.S.func(K) does not match any function
template declaration
 test2.d(44): Error: template test2.S.func(K) cannot deduce template function
from argument types !()(string)

 Removing 'inout' fixes the problem. But I don't understand why.


 T

return-type? Furthermore you don't the the inout here, since this template does match const/immutable/nothing anyways.
Mar 23 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 03/23/2012 11:58 PM, David wrote:
 Am 23.03.2012 23:52, schrieb H. S. Teoh:
 Code:
 struct S {
 int f(K)(K x) {
 return 1;
 }
 void func(K)(inout(K) x) {
 auto h = f(x);
 }
 }
 void main() {
 S s;
 s.func("abc"); // This is line 44
 }

 This refuses to compile:

 test2.d(44): Error: template test2.S.func(K) does not match any
 function template declaration
 test2.d(44): Error: template test2.S.func(K) cannot deduce template
 function from argument types !()(string)

 Removing 'inout' fixes the problem. But I don't understand why.


 T

return-type?

Not any more (DMD 2.059). See http://d.puremagic.com/issues/show_bug.cgi?id=7105
 Furthermore you don't the the inout here, since this
 template does match const/immutable/nothing anyways.

His intention probably is to prevent multiple instantiation of a template based on different constness.
Mar 23 2012
prev sibling parent "Kenji Hara" <k.hara.pg gmail.com> writes:
On Friday, 23 March 2012 at 23:01:43 UTC, Timon Gehr wrote:
 On 03/23/2012 11:58 PM, David wrote:
 Am 23.03.2012 23:52, schrieb H. S. Teoh:
 Code:
 struct S {
 int f(K)(K x) {
 return 1;
 }
 void func(K)(inout(K) x) {
 auto h = f(x);
 }
 }
 void main() {
 S s;
 s.func("abc"); // This is line 44
 }

 This refuses to compile:

 test2.d(44): Error: template test2.S.func(K) does not match 
 any
 function template declaration
 test2.d(44): Error: template test2.S.func(K) cannot deduce 
 template
 function from argument types !()(string)

 Removing 'inout' fixes the problem. But I don't understand 
 why.


 T

the return-type?

Not any more (DMD 2.059). See http://d.puremagic.com/issues/show_bug.cgi?id=7105
 Furthermore you don't the the inout here, since this
 template does match const/immutable/nothing anyways.

His intention probably is to prevent multiple instantiation of a template based on different constness.

This is definitely a bug. I've filed it in bugzilla, and posted a pull to fix it. http://d.puremagic.com/issues/show_bug.cgi?id=7769
Mar 24 2012