expl0it3r
expl0it3r

Reputation: 335

Using function return for another template function C++

I am trying to call a templated function using the return of another function, but I cannot seem to get it working.

enum class MYENUM {
    X1,X2
};

MYENUM SomeFunc() {
    return MYENUM::X1;
}

template<MYENUM T>
void ENUMFunc() {
    //do something
}

int main() {
    ENUMFunc<MYENUM::X1>(); //works
    ENUMFunc<SomeFunc()>(); //error?
}

Upvotes: 2

Views: 63

Answers (1)

NathanOliver
NathanOliver

Reputation: 181068

You can only use a constant expression for the value of a non-type template parameter. To get that, you need to mark SomeFunc as constexpr like

constexpr MYENUM SomeFunc() {
    return MYENUM::X1;
}

which will now let you use it for the template parameter as seen in this live example

Upvotes: 7

Related Questions