typeof and block statements
Mafi
mafi at example.org
Thu Aug 12 06:32:52 PDT 2010
Am 12.08.2010 15:13, schrieb simendsjo:
> The spec doesn't mention anything about block statements in typeof
> declarations.
>
> //typeof({1}) a; // found } expecting ;
> //typeof({1}()) b; // same as a
> typeof(1) c; // int
>
> I'm asking because isInputRange from std.range the idom from the b test:
>
> template isInputRange(R)
> {
> enum bool isInputRange = is(typeof(
> {
> R r; // can define a range object
> if (r.empty) {} // can test for empty
> r.popFront; // can invoke next
> auto h = r.front; // can get the front of the range
> }()));
> }
>
>
> Also... The unittest contains
> static assert(isInputRange!(int[]));
> static assert(isInputRange!(char[]));
>
> But arrays doesn't include these methods.. I don't understand a thing :(
The braces create an delegate which should be directly called after its
creation (this do the parens). If you put this into typeof, this
delegate will never be created, we just get the type of calling it. The
type of calling a delegate is of course the delegate's return type which
is automatically deduced.
Your exampley a and b are invalid beacuse '1' is not a valid statement
it's only a expression. 'return 1;' would work as expected.
Why does the spec don't mention it? It's beacuse a delegate is always a
valid expresion. typeof has no special case of braces.
Mafi
More information about the Digitalmars-d-learn
mailing list