www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Issues with templates

reply Artyom Shalkhakov <artyom.shalkhakov gmail.com> writes:
I'm trying to do byte-order conversions in compile-time rather than run-time,
since the latter case is rarely needed (if needed at all).

/// byte-order conventions (for uint conversion)
enum BYTEORDER {
	ARGB8,
	ABGR8,
	RGBA8,
	BGRA8
}

/// color, represented as RGB triple
struct Col3 {
	static Col3 opCall( float r, float g, float b ) {
		...
	}

        // byte-order is mostly static, so I tried to do this using templates
	static Col3 opCall( BYTEORDER order )( uint rgb ) {
		Col3	dst;

		// unpack according to byte-order
		static if ( order == ARGB8 ) {
			...
		}
		else static if ( order == ABGR8 ) {
			...
		}
		....
	}

	float	r, g, b;
}

compiler says:
template Col3.opCall( BYTEORDER order ) conflicts with function Col3.opCall at
...

Why can't I do this? I had to make a parameter for opCall to be able to compile
it.
I'm using Tango 0.99 RC3 / DMD 1.18 on Windows.
Aug 03 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Artyom Shalkhakov wrote:
 I'm trying to do byte-order conversions in compile-time rather than run-time,
since the latter case is rarely needed (if needed at all).
 
 /// byte-order conventions (for uint conversion)
 enum BYTEORDER {
 	ARGB8,
 	ABGR8,
 	RGBA8,
 	BGRA8
 }
 
 /// color, represented as RGB triple
 struct Col3 {
 	static Col3 opCall( float r, float g, float b ) {
 		...
 	}
 
         // byte-order is mostly static, so I tried to do this using templates
 	static Col3 opCall( BYTEORDER order )( uint rgb ) {
 		Col3	dst;
 
 		// unpack according to byte-order
 		static if ( order == ARGB8 ) {
 			...
 		}
 		else static if ( order == ABGR8 ) {
 			...
 		}
 		....
 	}
 
 	float	r, g, b;
 }
 
 compiler says:
 template Col3.opCall( BYTEORDER order ) conflicts with function Col3.opCall at
...
 
 Why can't I do this? I had to make a parameter for opCall to be able to
compile it.
 I'm using Tango 0.99 RC3 / DMD 1.18 on Windows.
Because you have a template and a function with the same symbol name. Let's remove the struct for a moment to get static Col3 opCall(float r, float g, float b); static Col3 opCall(BYTEORDER order)(uint rgb); What the compiler sees this as is: static Col3 opCall(float r, float g, float b); template Col3(BYTEORDER order) { static Col3(uint rgb); } That's like creating a class and a function with the same name: you can't do it. You also can't overload templated functions, at least not directly. You can make it work by turning the other function into a template and making sure it has a different number of template arguments: static Col3 opCall()(float r, float g, float b); static Col3 opCall(BYTEORDER order)(uint rgb); Note that the above isn't tested. Also note that, last time I checked, you can only have one templated member function with any given name, so it might not work at all. -- Daniel
Aug 03 2007
parent Artyom Shalkhakov <artyom.shalkhakov gmail.com> writes:
Daniel Keep Wrote:

(text omitted)

Thanks for pointing this one out to me. I'll try the way you described to see
if it works.
Aug 04 2007