Thread overview
Get return type of a template function without instantiating it
Nov 22, 2016
Yuxuan Shui
Nov 22, 2016
Nicholas Wilson
Nov 22, 2016
Jonathan M Davis
Nov 22, 2016
Meta
November 22, 2016
Is there a way to get a template function return type with instantiating it? The return type is independent of the template arguments.

I'm asking because there's potentially recursive template instantiation if I do try to instantiate it.
November 22, 2016
On Tuesday, 22 November 2016 at 12:21:18 UTC, Yuxuan Shui wrote:
> Is there a way to get a template function return type with instantiating it? The return type is independent of the template arguments.
>
> I'm asking because there's potentially recursive template instantiation if I do try to instantiate it.

Do you control the template in question's source? If so you could have a degenerate template type (e.g. MyTemplate!void ) that just returns the correct types .init

Otherwise i'm not sure you can because IIRC std.traits.ReturnType requires an instantiated symbol.
November 22, 2016
On Tuesday, November 22, 2016 12:21:18 Yuxuan Shui via Digitalmars-d-learn wrote:
> Is there a way to get a template function return type with instantiating it? The return type is independent of the template arguments.

No. There _is_ no function unless the template is instantiated. Remember that a templated function such as

auto foo(T)(T t)
    if(cond)
{
    ...
}

gets lowered to

template foo(T)
    if(cond)
{
    auto foo(T t)
    {
        ...
    }
}

Without instantiating the template, _nothing_ within the template actually exists except with regards to documentation generation. Not even the unittest blocks inside of a templated type exist until the type is instantiated. A template is just that - a template for code - not actual code to run, examine, or infer stuff from. It's only instantiations of the template that can be run, examined, or have stuff inferred about them.

> I'm asking because there's potentially recursive template instantiation if I do try to instantiate it.

If you want to avoid a recursive instantiation, then use a static if inside the template to break the recursion.

- Jonathan M Davis

November 22, 2016
On Tuesday, 22 November 2016 at 12:21:18 UTC, Yuxuan Shui wrote:
> Is there a way to get a template function return type with instantiating it? The return type is independent of the template arguments.
>
> I'm asking because there's potentially recursive template instantiation if I do try to instantiate it.

What you want cannot work in the general case. The template function must be instantiated.

T identity(T)(T t)
{
    return t;
}

It's not possible to calculate the type of the return value of `identity` until it is instantiated with a type.