如何写一个函数指针返回一个函数指针函数? [英] How to write a function pointer to a function returning a function pointer to a function?

查看:104
本文介绍了如何写一个函数指针返回一个函数指针函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为一个函数指针指定一个函数的地址,但是要处理的函数返回一个与它本身具有相同签名的函数指针,导致它以一种我根本不能写返回类型的方式进行递归,为函数指针甚至函数声明本身...

我想简化问题的一种方法,所以不会让人困惑:



我怎么能写一个函数声明,以便它可以返回一个指向它自己的指针(或者具有相同签名的任何其他函数)?

  ?????函数(int a){
//可以是此函数,或者具有相同签名的另一个函数,
返回任意函数;
}

(* func)(int)= function; //同上面的问题

编辑:



现在我有一个解决方案,但我不会将它作为答案发布,因为它非常难看。它通过简单地返回一个原始的 void * 指针作为返回类型,并最终采取以下形式来摆脱递归:

  void * function(int parameter){
return arbitraryFunction; //相同的签名
}

void *(* func)(int)= function;
func = reinterpret_cast< void *(*)(int)>(func(42)); // sin

edit2:

函数指针和常规指针之间的转换似乎是UB,所以在这种情况下我不能使用 void * ...



要回答其中一个注释,这是为了在我的程序中的多个主循环之间传递控制,每个循环获得它自己的函数。有很多方法可以做到这一点,但是在中间循环返回函数指针(或NULL来终止程序)似乎是最简单的方法,但我没有预料到指向数据和指针的指针函数地址将互相不兼容。我认为在这种情况下返回多态函数对象最终会成为更理智的选择。 解决方案

不要使用 void * ,因为不能保证 void * 可以存放函数指针。您可以使用 void(*)()作为解决方法:

  typedef void(* void_func)(); 
typedef void_func(* func_type)(int);
void_func任意函数(int a){
//可以是这个函数,或者另一个具有相同的签名,
cout<< arbitraryFunction\\\
;
返回nullptr;
}
void_func函数(int a){
//可以是此函数,或者具有相同签名的另一个函数,
return(void_func)arbitraryFunction;

int main(){
//你的代码在这里
func_type f =(func_type)function(0);
f(0);
返回0;
}

LIVE



C99 [6.2.5 / 27]:


指向void的指针与指向字符类型的指针具有相同的表示和对齐要求。同样,指向兼容类型的
合格或不合格版本的指针应具有
相同的表示和对齐要求。所有指向
结构类型的指针应该具有相同的表示形式和对齐
要求。所有指向union类型的指针都应该具有相同的
表示和对齐要求。指向其他类型的b $ b指针不需要具有相同的表示形式或对齐方式
要求。

C99 [6.3.2.3 / b]:


指向一种类型函数的指针可以被转换为指向另一种类型函数的指针并返回;结果应该比较
等于原始指针。



I want to assign a function's address to a function pointer, but the function to be addressed returns a function pointer with the same signature as itself, causing it to recurse in a way that I can't write the return type at all, for the function pointer or even the function declaration itself...

I guess a way of simplifying the problem so it's not confusing:

How could I write a function declaration such, that it can return a pointer to itself (or any other function with the same signature)?

????? function(int a){
    // could be this function, or another with the same signature, 
    return arbitraryFunction;  
}

?????(*func)(int) = function;  // same problem as above

edit:

For now I have a solution, though I won't post it as an answer because it's aggressively ugly. It gets rid of the recursion by simply returning a raw void* pointer as the return type, and ends up taking the following form:

void* function(int parameter){
    return arbitraryFunction; // of the same signature
}

void*(*func)(int) = function; 
func = reinterpret_cast<void*(*)(int)>(func(42));  // sin

edit2:

It seems casting between function pointers and regular pointers is UB, so I can't use void* in this case...

To answer one of the comments, this is for passing control between multiple "main" loops in my program, with each loop getting it's own function. There's a lot of ways to do this, but returning function pointers (or NULL to terminate the program) mid-loop seemed like the simplest method, but I didn't anticipate that pointers to data and pointers to function addresses would be incompatable with each other. I think returning polymorphic function objects will end up being the more sane option in this case.

解决方案

Don't use void*, because no guarantee that a void * can hold a function pointer. You can use void(*)() as a workaround:

typedef void(*void_func)();
typedef void_func (*func_type) (int);
void_func arbitraryFunction(int a) {
    // could be this function, or another with the same signature, 
    cout << "arbitraryFunction\n";
    return nullptr;  
}
void_func function(int a) {
    // could be this function, or another with the same signature, 
    return (void_func) arbitraryFunction;  
}
int main() {
    // your code goes here
    func_type f = (func_type) function(0);
    f(0);
    return 0;
}

LIVE

C99 [6.2.5/27]:

A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

C99 [6.3.2.3/8]:

A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer.

这篇关于如何写一个函数指针返回一个函数指针函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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