www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Check if template has been passed a reference type or value type?

reply "1967" <fdgs ff.com> writes:
I've got a template that takes in a type. Sometimes the type is a 
class, sometimes a struct, sometimes just an int. It doesn't much 
matter what it is, but if it's a reference type I need to check 
if it's null so I can make it not null before using it. I get an 
error if I try to check if a value type is null so I'd like to 
put a "static if" to check if the type passed to the template is 
a nullable one so I can avoid checking the value types passed in. 
What do I have to do to check for nullability?
Jun 07 2015
next sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Sunday, 7 June 2015 at 15:17:27 UTC, 1967 wrote:
 I've got a template that takes in a type. Sometimes the type is 
 a class, sometimes a struct, sometimes just an int. It doesn't 
 much matter what it is, but if it's a reference type I need to 
 check if it's null so I can make it not null before using it. I 
 get an error if I try to check if a value type is null so I'd 
 like to put a "static if" to check if the type passed to the 
 template is a nullable one so I can avoid checking the value 
 types passed in. What do I have to do to check for nullability?
You can directly check whether it allows comparison with null: static if(is(typeof(value is null) : bool)) { if(value is null) ... }
Jun 07 2015
parent "sigod" <sigod.mail gmail.com> writes:
On Sunday, 7 June 2015 at 15:39:17 UTC, Marc Schütz wrote:
 On Sunday, 7 June 2015 at 15:17:27 UTC, 1967 wrote:
 I've got a template that takes in a type. Sometimes the type 
 is a class, sometimes a struct, sometimes just an int. It 
 doesn't much matter what it is, but if it's a reference type I 
 need to check if it's null so I can make it not null before 
 using it. I get an error if I try to check if a value type is 
 null so I'd like to put a "static if" to check if the type 
 passed to the template is a nullable one so I can avoid 
 checking the value types passed in. What do I have to do to 
 check for nullability?
You can directly check whether it allows comparison with null: static if(is(typeof(value is null) : bool)) { if(value is null) ... }
Don't forget [`Nullable`][0] type, which won't pass this check.
Jun 24 2015
prev sibling parent "Gary Willoughby" <dev nomad.so> writes:
On Sunday, 7 June 2015 at 15:17:27 UTC, 1967 wrote:
 I've got a template that takes in a type. Sometimes the type is 
 a class, sometimes a struct, sometimes just an int. It doesn't 
 much matter what it is, but if it's a reference type I need to 
 check if it's null so I can make it not null before using it. I 
 get an error if I try to check if a value type is null so I'd 
 like to put a "static if" to check if the type passed to the 
 template is a nullable one so I can avoid checking the value 
 types passed in. What do I have to do to check for nullability?
A simple test: http://dpaste.dzfl.pl/3b8c5a0bb69c
Jun 25 2015