如何编写一个函数指针,返回一个返回函数指针的函数?

Ann*_*inn 10 c++ pointers function

我想将一个函数的地址赋给一个函数指针,但是要处理的函数返回一个与自身具有相同签名的函数指针,导致它以一种我根本无法写入返回类型的方式递归,因为函数指针甚至函数声明本身......

我想一种简化问题的方法,所以它不会混淆:

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

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

?????(*func)(int) = function;  // same problem as above
Run Code Online (Sandbox Code Playgroud)

编辑:

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

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

void*(*func)(int) = function; 
func = reinterpret_cast<void*(*)(int)>(func(42));  // sin
Run Code Online (Sandbox Code Playgroud)

EDIT2:

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

要回答其中一条注释,这是为了在程序中的多个"主"循环之间传递控制,每个循环都获得它自己的功能.有很多方法可以做到这一点,但返回函数指针(或NULL来终止程序)mid-loop 似乎是最简单的方法,但我没想到指向数据的指针和指向函数地址的指针与每个指针都不相容其他.我认为在这种情况下,返回多态函数对象将最终成为更合理的选项.

son*_*yao 6

不要使用void*,因为不能保证一个void *可以容纳一个函数指针.您可以将其void(*)()用作解决方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

生活

C99 [6.2.5/27]:

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

C99 [6.3.2.3/8]:

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

  • 它不能返回void,因为`func`需要匹配返回它的函数,它返回`func` (2认同)

Dav*_*lor 5

C中的技巧是利用任何类型的函数指针都可以强制转换为任何其他类型的函数指针:

#include <stdlib.h>
#include <stdio.h>

typedef void(*emptyfunc)(void);
typedef emptyfunc (*funcptr2)(int);

funcptr2 strategy(int m)
{
  printf("Strategy %d.\n", m);
  return (funcptr2)&strategy;
}

int main (void)
{
  const funcptr2 strategy2 = (funcptr2)strategy(1);
  const funcptr2 strategy3 = (funcptr2)strategy2(2);
  strategy3(3);

  return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

指向策略的每个指针始终处于可以调用一次的类型中,并且返回值将重新放回可以再次调用的表单中.

在C++中,您将声明一个函数对象:

class strategy {
  public:
  virtual const strategy& operator()(int) const = 0;
}
Run Code Online (Sandbox Code Playgroud)

可以像函数一样调用此类的实例.