Check template parameter whether it has "length"

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 8 04:28:48 PDT 2015


On 10/08/15 11:29, tcak via Digitalmars-d-learn wrote:
> 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.

Use `A.init.length` instead of `A.length`.


You could also use something like

   if(is(typeof(A.init.length):size_t))


artur


More information about the Digitalmars-d-learn mailing list