Thread overview
Checking if UFCS function exists for a specific type
Aug 29, 2013
Rory McGuire
Aug 29, 2013
Rory McGuire
Aug 30, 2013
Rory McGuire
August 29, 2013
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)"));
}
August 29, 2013
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
August 30, 2013
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