Trouble with template parameter matching

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 2 01:08:03 PDT 2015


[code]
void func1(N)( const N name )
	if( is(N: string) || is(N: char[]) )
{
	func2( name );
}

void func2(N)( const N name )
	if( is(N: string) || is(N: char[]) )
{}

void main(){
	char[] blah = ['b', 'l', 'a', 'h'];

	func1( blah );
	//func1( "blah" );	// this works
}
[/code]

[result]
test.d(4): Error: template test.func2 cannot deduce function from 
argument types !()(const(char[])), candidates are:
test.d(7):        test.func2(N)(const N name) if (is(N : string) 
|| is(N : char[]))
[/result]

When func1 is called with blah variable, I assume that N is 
char[].

 From there, when func2 is called by func1, name should be 
const(char[]).

But since func2 defined name parameter with const, shouldn't the 
compiler accept const part of const(char[]) as func2's const, and 
accept N as char[] still?

Otherwise, it is being weirdly recursive, and requires casting.


More information about the Digitalmars-d-learn mailing list