Thread overview | |||||
---|---|---|---|---|---|
|
April 05, 2011 ElementType!(Range) problem | ||||
---|---|---|---|---|
| ||||
I can use the ElementType() template in the std.rane library to find the type of the elements of a range,isn't it? But I cant compile the following programme. I get a "untitled.d(32): Error: template instance ElementType!(listR) does not match template declaration ElementType(R)" error. What could be the problem ? -------------------------------------------------------------------------- import std.stdio; import std.range; import std.container; int main(char[][] args) { auto list=SList!(int)(1,2,3); auto listR=list.opSlice(); writefln("%s",ElementType!(listR)); return 0; } -------------------------------------------------------------- Thank you...! |
April 05, 2011 Re: ElementType!(Range) problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ishan Thilina | > > I get a "untitled.d(32): Error: template instance ElementType!(listR) does not > match template declaration ElementType(R)" error. > > What could be the problem ? > -------------------------------------------------------------------------- > > import std.stdio; > import std.range; > import std.container; > > int main(char[][] args) > { > auto list=SList!(int)(1,2,3); > auto listR=list.opSlice(); > writefln("%s",ElementType!(listR)); > > > return 0; > } > -------------------------------------------------------------- ElementType acts on types. It takes a type and 'returns' (compiles to, actually) another type. You need to give it typeof(listR). Then, as ElementType!(typeof(listR)) is a type, you cannot pass it to writeln. Use .stringof to go from the type to a string representation of its name. So: writefln("%s", ElementType!(typeof(listR)).stringof); ElementType!(typeof(listR)) is a type like any other. You can create a variable with it: ElementType!(typeof(listR)) elem; Btw, .opSlice() is the name of the '[]' operator. You previous line can be written like this: auto listR = list[]; Philippe |
April 06, 2011 Re: ElementType!(Range) problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | --Philippe wrote:: >ElementType acts on types. It takes a type and 'returns' (compiles to, >actually) another type. You need to give it typeof(listR). >Then, as ElementType!(typeof(listR)) is a type, you cannot pass it to >writeln. Use .stringof to go from the type to a string representation >of its name. > >So: > >writefln("%s", ElementType!(typeof(listR)).stringof); > >ElementType!(typeof(listR)) is a type like any other. You can create a >variable with it: > >ElementType!(typeof(listR)) elem; > Got it!. Thanks. I faced this confusion because of the description for "ElementType" which is available in http://d-programming-language.org/phobos/std_range.html. It says, " template ElementType(R): The element type of R. R does not have to be a range. The element type is determined as the type yielded by r.front for an object r or type R. For example, ElementType!(T[]) is T. If R is not a range, ElementType!R is void. " So I thought that just passing ElementType!listR was adequate. :s |
Copyright © 1999-2021 by the D Language Foundation