Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a function return a pointer to its own type?

Tags:

I've implemented a very minimal finite state machine in a class, and my original approach was to try this:

class Widget {
public:
  void update(float time_interval){
    current_state = current_state_(time_interval);
  }
private:
  std::function<  ??  > off_(float);
  std::function<  ??  > standby_(float);
  std::function<  ??  > locked_(float);

  std::function<  ??  > current_state_; // initialised to off_
};

Each state is a function which returns a state. But I can't work out how to declare a function whose return type includes its return type. Is there a way to break the recursion?

Instead, I used an enum class and an ugly switch statement.

like image 782
Jack Deeth Avatar asked Mar 30 '18 13:03

Jack Deeth


People also ask

Can a function return a function pointer?

Return Function Pointer From Function: To return a function pointer from a function, the return type of function should be a pointer to another function. But the compiler doesn't accept such a return type for a function, so we need to define a type that represents that particular function pointer.

Can we declare a function that can return a pointer to a function of the same type a yes b no?

You cannot return a function in C - you return a pointer to a function. If you mean to define a function which returns a pointer to a function which again returns a pointer to a function and so on, then you can use typedef to implement it.

What is the type of a pointer to function?

The type of a pointer to a function is based on both the return type and parameter types of the function. In the first declaration, f is interpreted as a function that takes an int as argument, and returns a pointer to an int .

Can a pointer be a function?

In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.


1 Answers

If I understand correctly, you want to return a function reference to the next function in the state chain, and that all the state step functions have identical signature?

I see the problem here is that the return type is the function itself, so declaring the type of the function invokes a recursive type definition.

So you want to write functions of the form RetType fn_(float) but the type of RetType is (basically) RetType fn_(float). So now we have (RetType (*)(float)) fn_(float) or something like that, but however hard we try we can't get rid of RetType.

You can't shut down that recursion without forward declaring something. We can forward declare classes, and use them, so let's write a simple class wrapper for your function pointer, which is perhaps what @Jarod was alluding to. Now, std::function is sort-of a class wrapper for your function, but it needs the explicit type declaration, which we don't have.

class Widget;
class StateFn;
class StateFn
{
  Widget * THIS;
  StateFn (Widget::*Fn)(float);
public:
  /// Or you could rely on POD construction
  StateFn(Widget * THIS, StateFn (Widget::*Fn)(float))
  : THIS(THIS), Fn(Fn)
  {}
  StateFn operator()(float f)
  {
    return THIS->*fn(f);
  }
};

So now the recursive definition is broken, our state functions can return StateFn objects, and we can call on them.

like image 200
Gem Taylor Avatar answered Oct 11 '22 17:10

Gem Taylor