[英]Type alias and alias template with function datatype
template<typename R, typename... Types>
using Function = R(*)(Types...);
I saw this lines on dev.to 我在dev.to上看到了这条线
This type alias declaration R(*)(Types...)
looks weird to me because there is no function name pointer. 这种类型别名声明
R(*)(Types...)
在我看来很奇怪,因为没有函数名指针。
What is this and how can I implement this template? 这是什么?如何实现此模板?
This type alias declaration
R(*)(Types...)
looks weird to me because there is no function name pointer.这种类型别名声明
R(*)(Types...)
在我看来很奇怪,因为没有函数名指针。
There is no name since it is supposed to be a type. 没有名称,因为它应该是一种类型。 Take a simpler example:
举一个简单的例子:
using IPtr = int*;
The RHS is just a type. RHS只是一种类型。 It won't make sense to use
使用没有意义
using IPtr = int* x;
What is this and how can I implement this template?
这是什么?如何实现此模板?
It enables declaring and defining of function pointers using a simple and intuitive syntax. 它允许使用简单直观的语法声明和定义函数指针。
You can use 您可以使用
int foo(double) { ... }
int bar(double) { ... }
Function<int, double> ptr = &foo;
// Use ptr
// Change where ptr points to
ptr = &bar;
// Use ptr more
I find example uses on https://en.cppreference.com/w/cpp/language/type_alias 我在https://en.cppreference.com/w/cpp/language/type_alias上找到了示例用法
// type alias, identical to
// typedef void (*func)(int, int);
using func = void (*) (int, int);
// the name 'func' now denotes a pointer to function:
void example(int, int) {}
func f = example;
And this lines are implementation for actual question; 这行是针对实际问题的实现;
template<typename R, typename... Types>
using Function = R(*)(Types...);
bool example(int&a){
cout<< a;
return false;
}
int main()
{
int a = 8;
Function<bool, int&> eFnc = example;
eFnc(a);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.