opCast / operator overloading with additional template arguments

Paul paultjeadriaanse at gmail.com
Mon Jan 11 01:09:21 UTC 2021


On Monday, 11 January 2021 at 00:25:36 UTC, Ali Çehreli wrote:
> You don't show complete code; so, I hope I came up with 
> something that reflects your case.

Thank you, sadly S (S2 now) is not any specific type, sorry I'll 
paste more of my file, I hope that's ok. (Sidenote, I'm not sure 
it's the most elegant approach to have a templated union like 
this, and I left out some unnecessary stuff like 'opBinary')

> version (HoekjeD_Double) {
> 	private alias standard_accuracy = double;
> } else {
> 	private alias standard_accuracy = float;
> }
> 
> struct Vec(int size, S = standard_accuracy) {
> 	union {
> 		S[size] content;
> 		static if (size >= 1) {
> 			struct {
> 				S x;
> 				static if (size >= 2) {
> 					S y;
> 					static if (size >= 3) {
> 						S z;
> 						static if (size >= 4)
> 							S w;
> 					}
> 				}
> 			}
> 		}
> 	}
> 
> 	T opCast(T)() const if (is(T : Vec!(size, S2), S2)) {
> 		T converted;
> 		static foreach (i; 0 .. size)
> 			converted.content[i] = cast(S2) content[i];
> 		return converted;
> 	}
> }


More information about the Digitalmars-d-learn mailing list