www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Checking for possibility of implicit conversions

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
How do I check if a given type T can be implicitly converted to some
type S at compile-time? I'm trying to write a signature constraint for a
template function that should only be instantiated if the parameter type
can be implicitly assigned to some given type S.

	struct S(T) {
		T value;

		void setValue(S)(S newValue)
			if ( /* ???? */ )
		{
			value = newValue;
		}

I tried __traits(compiles, value = newValue) but it seems to always
return true, and then later the compiler errors out at the actual
assignment statement. I'd like to be able to catch this at the signature
constraint instead of inside the function body.


T

-- 
If you look at a thing nine hundred and ninety-nine times, you are
perfectly safe; if you look at it the thousandth time, you are in
frightful danger of seeing it for the first time. -- G. K. Chesterton
Mar 14 2012
next sibling parent =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 14-03-2012 18:07, H. S. Teoh wrote:
 How do I check if a given type T can be implicitly converted to some
 type S at compile-time? I'm trying to write a signature constraint for a
 template function that should only be instantiated if the parameter type
 can be implicitly assigned to some given type S.

 	struct S(T) {
 		T value;

 		void setValue(S)(S newValue)
 			if ( /* ???? */ )
 		{
 			value = newValue;
 		}

 I tried __traits(compiles, value = newValue) but it seems to always
 return true, and then later the compiler errors out at the actual
 assignment statement. I'd like to be able to catch this at the signature
 constraint instead of inside the function body.


 T

http://dlang.org/phobos/std_traits.html#isImplicitlyConvertible -- - Alex
Mar 14 2012
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
if(is(T : S))

http://dlang.org/expression.html#IsExpression

this is form #2.
Mar 14 2012