www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template condition triggers compiler error?

reply "Namespace" <rswhite4 googlemail.com> writes:
This code compiles:
----
template gc_free(T) {
	static if (is(T : U*, U) || is(T : U[], U))
		alias Type = T;
	else
		alias Type = T*;

	void gc_free(Type data) {
		import core.memory : GC;

		static if (is(Type : U[], U)) {
			GC.free(data.ptr);
			GC.minimize();
		} else {
			GC.free(data);
		}

		data = null;
	}
}
----

But with a template condition it triggers compiler errors:
----
template gc_free(T) if (!is(T == class)) {
	static if (is(T : U*, U) || is(T : U[], U))
		alias Type = T;
	else
		alias Type = T*;

	void gc_free(Type data) {
		import core.memory : GC;

		static if (is(Type : U[], U)) {
			GC.free(data.ptr);
			GC.minimize();
		} else {
			GC.free(data);
		}

		data = null;
	}
}
----

Errors:
share.d(10): Error: undefined identifier Type
share.d(10): Error: undefined identifier Type
share.d(177): Error: template instance share.share!(A) error 
instantiating
share.d(10): Error: undefined identifier Type
share.d(10): Error: undefined identifier Type
share.d(197): Error: template instance share.share!(A) error 
instantiating
share.d(10): Error: undefined identifier Type
share.d(10): Error: undefined identifier Type
share.d(10): Error: undefined identifier Type
share.d(10): Error: undefined identifier Type
share.d(10): Error: undefined identifier Type
share.d(10): Error: undefined identifier Type

It seems to work on Linux with 64 bit, but it fails on Windows 7 
32 bit.

Bug, feature or my fault?
Dec 01 2013
parent "Kenji Hara" <k.hara.pg gmail.com> writes:
On Sunday, 1 December 2013 at 16:07:18 UTC, Namespace wrote:
 This code compiles:
 ----
 template gc_free(T) {
 	static if (is(T : U*, U) || is(T : U[], U))
 		alias Type = T;
 	else
 		alias Type = T*;

 	void gc_free(Type data) {
 		import core.memory : GC;

 		static if (is(Type : U[], U)) {
 			GC.free(data.ptr);
 			GC.minimize();
 		} else {
 			GC.free(data);
 		}

 		data = null;
 	}
 }
 ----

 But with a template condition it triggers compiler errors:
 ----
 template gc_free(T) if (!is(T == class)) {
 	static if (is(T : U*, U) || is(T : U[], U))
 		alias Type = T;
 	else
 		alias Type = T*;

 	void gc_free(Type data) {
 		import core.memory : GC;

 		static if (is(Type : U[], U)) {
 			GC.free(data.ptr);
 			GC.minimize();
 		} else {
 			GC.free(data);
 		}

 		data = null;
 	}
 }
 ----

 Errors:
 share.d(10): Error: undefined identifier Type
 share.d(10): Error: undefined identifier Type
 share.d(177): Error: template instance share.share!(A) error 
 instantiating
 share.d(10): Error: undefined identifier Type
 share.d(10): Error: undefined identifier Type
 share.d(197): Error: template instance share.share!(A) error 
 instantiating
 share.d(10): Error: undefined identifier Type
 share.d(10): Error: undefined identifier Type
 share.d(10): Error: undefined identifier Type
 share.d(10): Error: undefined identifier Type
 share.d(10): Error: undefined identifier Type
 share.d(10): Error: undefined identifier Type

 It seems to work on Linux with 64 bit, but it fails on Windows 
 7 32 bit.

 Bug, feature or my fault?
Compiler bug. https://d.puremagic.com/issues/show_bug.cgi?id=11662 Kenji Hara
Dec 01 2013