Trouble with template parameter matching
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Aug 2 05:16:03 PDT 2015
On Sunday, 2 August 2015 at 08:08:05 UTC, tcak wrote:
> [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[].
Yup.
> From there, when func2 is called by func1, name should be
> const(char[]).
Yup.
> 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?
Nope. When removing the top level const of `const(char[])`, it
becomes `const(char)[]`. Test for that in your template
constraint and it works.
More information about the Digitalmars-d-learn
mailing list