Testing for template argument being result of takeExactly
Ali Çehreli
acehreli at yahoo.com
Sun Sep 23 13:57:38 PDT 2012
On 09/23/2012 12:02 PM, monarch_dodra wrote:
> On Saturday, 22 September 2012 at 23:53:28 UTC, Jonathan M Davis wrote:
>> I'm trying to test...
>> [SNIP]
>> - Jonathan M Davis
>
> I *kind of* see what you are doing with U, V, W, but what's wrong with
> just doing:
>
> ----
> import std.range;
>
> template Hello(R)
> if(is(typeof(R == typeof(takeExactly(R, 1)))))
> {
> alias R Hello;
> }
>
> struct S;
>
> void main( ) {
> Hello!(int[]) a; //OK
> Hello!S b; //FAIL
> }
> ----
> ?
>
> It seems to work for me..., but I'm not 100% sure there isn't something
> illegal/unsafe in there.
The goal is to "test whether a template argument is the type returned by
takeExactly."
Your solution (I think) is looking at a container and determining
whether the type of that container can be compared to the return type of
takeExactly(). (Note that typeof(X == Y) is used for checking whether
that expression itself is valid.)
Also, I think your code should have passed a range like R.init to
takeExactly, not R, which is a type:
takeExactly(R.init, 1)
I don't know why your code compiles.
Ali
More information about the Digitalmars-d-learn
mailing list