Get return type of a template function without instantiating it

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 22 05:04:45 PST 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



More information about the Digitalmars-d-learn mailing list