ReturnType and overloaded functions

Freddy via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 12 18:44:12 PDT 2015


> I am curious about the answer myself but there is the 
> workaround of passing the overload through a lambda:
>
> import std.traits;
>
> int foo(long)
> {
>     return 0;
> }
>
> short foo(byte)
> {
>     return 0;
> }
>
> void main()
> {
>     static assert(is (ReturnType!(() => foo(long.init)) == 
> int));
>     static assert(is (ReturnType!(() => foo(byte.init)) == 
> short));
> }
>
> Ali

Why not just use templates?

int foo(size_t id)() if(id == 0){
	return 0;
}

short foo(size_t id)() if(id == 1){
	return 0;
}

static assert(is(typeof(foo!0()) == int));
static assert(is(typeof(foo!1()) == short));


More information about the Digitalmars-d mailing list