已知类型的别名功能模板 [英] Aliasing function template with known type

查看:86
本文介绍了已知类型的别名功能模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想为c ++中的已知类型使用模板类的别名,我可以执行以下操作:

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

我尝试过:

using MyIntFunction = MyFunction<int>;

但是它不起作用。

推荐答案

别名声明旨在为类型引入别名。

Alias declarations are meant to introduce aliases for types.

无论如何,您可以使用 constexpr 变量来做您要尝试做的事情(我怀疑):

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

这篇关于已知类型的别名功能模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆