Creating a template mixin for explicit casts.

Sjoerd Nijboer sjoerdnijboer at gmail.com
Thu May 17 15:25:37 UTC 2018


Given the following code
`struct Foo(T)
	if(isNumeric!T)
{
	T t;
	.. other code
}

struct Bar(T)
	if(isNumeric!T)
{
	T t;
	.. other code
}

Foo!float foo_float;

Foo!double foo_double;

Bar!float bar_float;
`

I want to make a template mixin that is able to cast one of these 
generic structs to the other explicitly. I have a bunch of these 
structs and therefore I thought it would make sense to do it by 
template mixin. I just don't know what language or library 
features I need to use and how to apply them.

`
fooDouble = cast (Foo!double) foo_float;		// case [1] if 
possible, done by
							// an implicit cast. Else by
							//explicit.

fooFloat = cast (Foo!float) foo_double;			// case [2]

barFloat = cast (Bar!float) foo_float;			// case [3]
`

How would I do this in D?


More information about the Digitalmars-d-learn mailing list