On Fri, Apr 1, 2011 at 5:14 PM, enuhtac <enuhtac_lists@gmx.de> wrote:
Hello,
the "is" expression is a great feature of D - but its use is not very
intuitive, at least for me.
I'm trying to write a template that figures out if the template
parameter is of a given type.
This is the type I would like to check for:
struct A( T, string s )
{ ... };
One possibility to accomplish this check is explicit template
specialization:
template isA( T )
{
enum bool isA = false;
};
template isA( T : A!( U, s ), U, string s )
{
enum bool isA = true;
};
This more or less the C++ approach. But in D this could also be done
with static if and the "is" expression. As I understand "is" it should
be done like this:
template isA( T )
{
static if( is( T U == A!( U, s ), string s ) )
enum bool isA = true;
else
enum bool isA = false;
};
But this does not work. So what am I doing wrong?
Regards,
enuhtac
I'm new too, but I think it should be like this:
template isA( T ){
enum bool isA = is(T : A)
}
if the name of enum is same as the template then you could use it as such:
if( isA( T ) ){ }
instead of
if( isA( T ).isA ){ }
Also note that : allows implicit conversion, while == requires the
types to be exactly the same.
Your right. In your example it is possible to circumvent the "static
if" construct. I was not aware of this, so thanks for the hint.