Kazade
Kazade

Reputation: 1327

Returning reference to a function template

I have a template class called Managed which has a static templated create() function. I'm trying to write a helper which returns that function when passed the Managed subclass type.

typedef std::function<ScreenBase::ptr (WindowBase&)> ScreenFactory;

template<typename T>
ScreenFactory screen_factory() {
    ScreenFactory ret = &T::create<WindowBase&>;
    return ret;
}

Because create itself takes template arguments, I explicitly try to get a reference to the instantiation that takes a WindowBase&. As far as I can see the above code should work.

However, when I try to call it, I get the following error:

error: expected '(' for function-style cast or type construction
ScreenFactory ret = &T::create<WindowBase&>;
                               ~~~~~~~~~~^

I'm not sure what I'm doing wrong, any pointers?

Upvotes: 0

Views: 92

Answers (1)

Piotr Skotnicki
Piotr Skotnicki

Reputation: 48447

You are missing a template keyword:

template<typename T>
ScreenFactory screen_factory() {
    ScreenFactory ret = &T::template create<WindowBase&>;
    //                      ~~~~~~~^
    return ret;
}

For details please refer to Where and why do I have to put the “template” and “typename” keywords?

Upvotes: 4

Related Questions