Aliasing function template with known type

不打扰是莪最后的温柔 提交于 2019-12-12 11:20:12

问题


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.


回答1:


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);
}



回答2:


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



来源:https://stackoverflow.com/questions/38284076/aliasing-function-template-with-known-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!