[英]Template + Typedef C++
if we have a line of code : 如果我们有一行代码:
typedef bool (*cmp_func)(int i0, int i1);
How to change this to : 如何将其更改为:
typedef bool (*cmp_func)(T i0, T i1);
where T - typename ? T型名在哪里?
Since C++11: 从C ++ 11开始:
template <typename T>
using cmp_func = bool (*)(T, T);
There is no template typedef, but you can have a typedef inside a template: 没有模板typedef,但是您可以在模板中包含typedef:
template <typename T>
struct Foo {
typedef bool (*cmp_func)(T i0, T i1);
};
int main() {
Foo<int>::cmp_func f;
}
Or since C++11 you can do the same via type aliasing 或者从C ++ 11开始,您可以通过类型别名进行相同的操作
template <typename T>
using cmp_func = bool(*)(T,T);
Also note that even though C++11 has type aliasing, this comes with some restrictions (in particular they cannot be specialized) and sometimes it is necessary to combine both approaches. 还要注意,即使C ++ 11具有类型别名,它也有一些限制(特别是它们不能专门化),有时必须将两种方法结合起来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.