Const is already there. It cannot deduce it

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 17 15:14:09 PDT 2015


On Sunday, 17 May 2015 at 21:34:21 UTC, tcak wrote:
> [code]
> void test(D)( const D data ) if( is(D: shared(char[]) ) ) { }
>
> void main() {
> 	char[] text = new char[4];
> 	text[0] = 'a'; text[1] = 'b'; text[2] = 'c'; text[3] = 'd';
> 	
> 	auto t = cast( shared(const(char[])) )text[1..2];
>
> 	test( t );
> }
>
> [/code]
>
> Error Message:
> template main.test cannot deduce function from argument types 
> !()(shared(const(char[])))
>
> `t` is already shared(const(char[])), and `test` is expecting 
> const shared(char[]). Aren't they already same?
>

Per the template constraint, test needs a type D that implicitly 
converts to shared(char[]). Even when the top const is removed 
from typeof(t), there's still const in there: 
shared(const(char)[]). That isn't implicitly convertible to 
shared(char[]). Add const in the constraint and it works:
if( is(D: shared(const char[])) )

> --
>
> Together with this question, I want to ask whether there is a 
> way to check only being `shared`,

is(T == shared)

> only being `const`,

is(T == const)

> or only being `char[]`

I guess you mean to ignore any qualifiers of the array itself and 
the element type.

We have std.traits.Unqual, but that only removes qualifiers from 
the top level. As far as I know, we don't have anything like a 
DeepUnqual which would allow you to write is(DeepUnqual!T == 
char[]). You could write one.

Or you can get into the more advanced versions of the 
IsExpression:
is(T == E[], E) && is(Unqual!E == char)

> of a template variable (D of `test` in this case)?


More information about the Digitalmars-d-learn mailing list