Check template parameter whether it has "length"
tcak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Oct 8 02:29:29 PDT 2015
I am "trying" to write a function that takes an array of items,
and returns the length of longest item.
[code]
size_t maxLength(A)( const A[] listOfString ) if( __traits(
hasMember, A, "length" ) )
{
return 0; // not implemented yet
}
[/code]
I tried it with
if( __traits( compiles, A.length ) )
as well. But compiler doesn't match it.
writeln("Max Length: ", maxLength( ["foo", "123456789"] ));
Compilers says it cannot deduce function from argument types ...
I do not want to check whether the type "A" is string, char[],
etc. As long as it has length (please do not put me into ranges,
library functions etc as much as possible), I want the function
to accept it.
More information about the Digitalmars-d-learn
mailing list