www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Deduction of Template Value Parameters

reply gg <noreply now.pl> writes:
Why we cant deduce value parameter of templates:
void caracterize(uint m)(pp!(m)){
	
}
class pp(uint m){

}
void main(){
	caracterize(pp!5);
}
fails with

dmd chap.d
chap.d(8): Error: template chap.caracterize(uint m) does not match any function
template declaration
chap.d(8): Error: template chap.caracterize(uint m) cannot deduce template
function from argument types !()(void)

Or at least of a way to extract template instantiation information would be
useful. Or  it is needed to do it the long way
Nov 02 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
gg:

 Why we cant deduce value parameter of templates:
 void caracterize(uint m)(pp!(m)){
 	
 }
 class pp(uint m){
 
 }
 void main(){
 	caracterize(pp!5);
 }
You are doing it wrong. Try: caracterize(new pp!5U()); You need to build the class instance with new, and if you want an uint you need to add a U. You may also ask similar questions in the D.learn group. Bye, bearophile
Nov 02 2010
parent reply gg <g g.com> writes:
 You need to build the class instance with new, and if you want an uint you
need to add a U.
 
 You may also ask similar questions in the D.learn group.
 
 Bye,
 bearophile
Sorry,i must check before posting, I tried to reduce to a simple test case al long code. This is THE an actual confusion: FAILS class Matrix (Tx,uint x, uint y){ } void tes(T,uint x,uint y)(Matrix!(T,x,y) x){ } void main(){ auto g = new Matrix!(double,5,4)();//Here should be the error.! tes(g);// WHY fails here? } fails Error: cannot implicitly convert expression (g) of type chap.Matrix!(4).Matrix to chap.Matrix!(x).Matrix WORKS class Matrix (Tx,uint x, uint y){ } void tes(T,uint x,uint y)(Matrix!(T,x,y) x){ } void main(){ auto g = new Matrix!(double,5u,4u)(); tes(g); } The actual question why 5 and 4 and 5u and 4u are accepted in template instantiation but only one is valid
Nov 02 2010
parent bearophile <bearophileHUGS lycos.com> writes:
gg:

 The actual question why 5 and 4 and 5u and 4u are accepted in template
instantiation but only one is valid 
It may be a compiler bug. If confirmed you (or someone else) may put it into Bugzilla. Bye, bearophile
Nov 03 2010
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 02 Nov 2010 21:56:44 -0400, gg <noreply now.pl> wrote:

 Why we cant deduce value parameter of templates:
 void caracterize(uint m)(pp!(m)){
 	
 }
 class pp(uint m){

 }
 void main(){
 	caracterize(pp!5);
 }
 fails with

 dmd chap.d
 chap.d(8): Error: template chap.caracterize(uint m) does not match any  
 function template declaration
 chap.d(8): Error: template chap.caracterize(uint m) cannot deduce  
 template function from argument types !()(void)

 Or at least of a way to extract template instantiation information would  
 be useful. Or  it is needed to do it the long way
I think there is a way to do this, but I think you have to include the pp!(m) parameter, something like: caracterize(m, T : pp!m)(pp!(m) arg) Look through phobos' source, you will probably find someone else has solved the problem. -Steve
Nov 03 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 03 Nov 2010 10:19:28 -0400, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 On Tue, 02 Nov 2010 21:56:44 -0400, gg <noreply now.pl> wrote:

 Why we cant deduce value parameter of templates:
 void caracterize(uint m)(pp!(m)){
 	
 }
 class pp(uint m){

 }
 void main(){
 	caracterize(pp!5);
 }
 fails with

 dmd chap.d
 chap.d(8): Error: template chap.caracterize(uint m) does not match any  
 function template declaration
 chap.d(8): Error: template chap.caracterize(uint m) cannot deduce  
 template function from argument types !()(void)

 Or at least of a way to extract template instantiation information  
 would be useful. Or  it is needed to do it the long way
I think there is a way to do this, but I think you have to include the pp!(m) parameter, something like: caracterize(m, T : pp!m)(pp!(m) arg)
Hm... that should have been: caracterize(m, T : pp!m)(T arg) -Steve
Nov 03 2010