Aliasing function template with known type

2019-06-26 17:33发布

If I want to alias a template class for a known type in c++, I do something like this :

using MyVector = std::vector<MyClass>;

How do I ahieve the same for function templates?

template <typename T> void MyFunction(T MyValue);

I tried :

using MyIntFunction = MyFunction<int>;

But its not working.

2条回答
欢心
2楼-- · 2019-06-26 18:11

Alias declarations are meant to introduce aliases for types.

Anyway, you can use a constexpr variable to do what (I suspect) you are trying to do:

constexpr auto MyIntFunction = &MyFunction<int>;

It follows a minimal, working example:

#include<iostream>

template <typename T>
void MyFunction(T MyValue) {
    std::cout << MyValue << std::endl;
}

constexpr auto MyIntFunction = &MyFunction<int>;

int main() {
    MyIntFunction(42);
}
查看更多
别忘想泡老子
3楼-- · 2019-06-26 18:30

You can not use template aliases for functions, there is no such syntax.

查看更多
登录 后发表回答