简体   繁体   中英

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?
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM