Testing for template argument being result of takeExactly

monarch_dodra monarchdodra at gmail.com
Mon Sep 24 01:02:54 PDT 2012


On Monday, 24 September 2012 at 07:51:23 UTC, Jonathan M Davis 
wrote:
> On Monday, September 24, 2012 09:41:26 monarch_dodra wrote:
>> Regarding the ".init" issue, I hadn't thought of that, but it 
>> can
>> be worked around pretty easily with an is(R r):
>> 
>> --------
>> template Hello(R)
>>       if ( is(R r) &&
>>            is(typeof(takeExactly(r, 1))) &&
>>            is(R == typeof(takeExactly(r, 1)))
>>       )
>> {
>>       alias R Hello;
>> }
>> --------
>
> That was one trick that I was not aware of. I didn't think that 
> one is
> expression could have an effect on a later one in the 
> surrounding expression.
> Cool. Though I would point out that that probably doesn't avoid 
> the init
> problem, because R r uses R.init (unless is expressions treat 
> it differently,
> which they may). The normal way to avoid that is to do
>
> R r = void;
>
> but I don't think that that would work in that is expression. 
> Sometimes
> disabling init is useful, but it can definitely be problematic. 
> It may
> ultimately have been a mistake to allow it. I don't know.
>
> - Jonathan M Davis

Well, it does work...

struct S
{
     @disable this();
}

void foo(T)(T i)
     if ( is(T t))
{}

void main()
{
     //S s; //Fail
     S s = void;
     foo(s); //Works
}

I think it makes sense that it works, because "is" doesn't 
actually validate a compile time syntax, rather it is just 
declaring that "t can be used as an instance of T", but it is not 
actually declaring *the variable* "t" itself... Not sure I'm 
explaining myself.

Or I think that's how it works. I've been on a wrong streak 
lately :/


More information about the Digitalmars-d-learn mailing list