www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Checking if UFCS function exists for a specific type

reply "Rory McGuire" <rjmcguire gmail.com> writes:
Hi all,

I've got this little ctfe template function that checks if a 
function called member with first argument T exists. Its for 
checking if a type has a custom encoder.

bool hasUFCSmember(T, string member)() {
	T v;
	// would be nice if we could use ParameterTypeTuple to get the 
first arg and check for exact type match
	
	return __traits(compiles, mixin(member ~"(v)"));
}
Aug 29 2013
parent reply "Rory McGuire" <rjmcguire gmail.com> writes:
On Thursday, 29 August 2013 at 21:06:04 UTC, Rory McGuire wrote:
 Hi all,

 I've got this little ctfe template function that checks if a 
 function called member with first argument T exists. Its for 
 checking if a type has a custom encoder.

 bool hasUFCSmember(T, string member)() {
 	T v;
 	// would be nice if we could use ParameterTypeTuple to get the 
 first arg and check for exact type match
 	
 	return __traits(compiles, mixin(member ~"(v)"));
 }
forum posted when I hit tab. Meant to ask: Is there a way to make sure that the compiler is not implicitly casting T? Currently it returns true for bool, enum, int etc... even if the only function available is for an int. I also have: template hasUFCSmember(T, string member) { enum hasUFCSmember = __traits(compiles, mixin(member ~"(T.init)")); } But it doesn't work for types such as string[string] because T.init is null. Any help will be greatly appreciated. Thanks, R
Aug 29 2013
parent "Rory McGuire" <rjmcguire gmail.com> writes:
On Thursday, 29 August 2013 at 21:10:41 UTC, Rory McGuire wrote:
 On Thursday, 29 August 2013 at 21:06:04 UTC, Rory McGuire wrote:
 Hi all,

 I've got this little ctfe template function that checks if a 
 function called member with first argument T exists. Its for 
 checking if a type has a custom encoder.

 bool hasUFCSmember(T, string member)() {
 	T v;
 	// would be nice if we could use ParameterTypeTuple to get 
 the first arg and check for exact type match
 	
 	return __traits(compiles, mixin(member ~"(v)"));
 }
forum posted when I hit tab. Meant to ask: Is there a way to make sure that the compiler is not implicitly casting T? Currently it returns true for bool, enum, int etc... even if the only function available is for an int. I also have: template hasUFCSmember(T, string member) { enum hasUFCSmember = __traits(compiles, mixin(member ~"(T.init)")); } But it doesn't work for types such as string[string] because T.init is null. Any help will be greatly appreciated. Thanks, R
I suppose I can avoid the implicit casts for function lookup by using Typedef. Thanks, R
Aug 30 2013