Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to declare a pointer to a function with unknown (at compile time) return type

I have a class A in which I want to have a pointer to a function as data member:

class A
{
  protected:
    double (*ptrToFunction) ( double );

  public:
  ...
  //Setting function according to its name
  void SetPtrToFunction( std::string fName );
};

But what if I want ptrToFunction to be sometimes double and sometimes - int, to have something like:

//T is a typename
T(*ptrToFunction) ( double );

How should I declare it in this case?

like image 982
paraxod Avatar asked Sep 09 '25 19:09

paraxod


2 Answers

A discriminated union can do that for you:

class A
{
  template<T>
  using cb_type = T(double);

  protected:
     enum {IS_INT, IS_DOUBLE} cb_tag;
     union {
       cb_type<int>    *ptrToIntFunction;
       cb_type<double> *ptrToDoubleFunction;
     };

  public:
  ...
  // Setting function according to its name
  void SetPtrToFunction( std::string fName );
};

A more general and elegant solution for a discriminated union can be applied with std::variant in C++17, or boost::variant for earlier standard revisions.


Alternatively, if you want to completely ignore the return type, you can convert the member into std::function<void(double)> and benefit from type erasure. The Callable concept will see the call via pointer converted into static_cast<void>(INVOKE(...)) and discard the return value, whatever it is.

To illustrate:

#include <functional>
#include <iostream>

int foo(double d)  { std::cout << d << '\n'; return 0; }

char bar(double d) { std::cout << 2*d << '\n'; return '0'; }

int main() {
    std::function<void(double)> cb;

    cb = foo; cb(1.0);

    cb = bar; cb(2.0);

    return 0;
}

And finally, if you do care about the return value but don't want to store a discriminated union. Then knowing about unions and the behavior of std::function, you can combine the above two approaches.

Like this

#include <functional>
#include <iostream>
#include <cassert>

int    foo(double d) { return d; }

double bar(double d) { return 2*d; }

struct Result {
    union {
        int    i_res;
        double d_res;
    };
    enum { IS_INT, IS_DOUBLE } u_tag;

    Result(Result const&) = default;
    Result(int i)  : i_res{i}, u_tag{IS_INT} {}
    Result(double d) : d_res{d}, u_tag{IS_DOUBLE} {}

    Result& operator=(Result const&) = default;
    auto& operator=(int i)
    { i_res = i; u_tag = IS_INT;    return *this; }
    auto& operator=(double d)
    { d_res = d; u_tag = IS_DOUBLE; return *this; }
};

int main() {
    std::function<Result(double)> cb;

    cb = foo;
    auto r = cb(1.0);
    assert(r.u_tag == Result::IS_INT);
    std::cout << r.i_res << '\n';

    cb = bar;
    r = cb(2.0);
    assert(r.u_tag == Result::IS_DOUBLE); 
    std::cout << r.d_res << '\n';

    return 0;
}
like image 149
StoryTeller - Unslander Monica Avatar answered Sep 13 '25 04:09

StoryTeller - Unslander Monica


If your class doesn't have a template, as in your example, you could do this:

template <class T>
struct myStruct
{
  static T (*ptrToFunction)(double);
};
like image 21
gsamaras Avatar answered Sep 13 '25 06:09

gsamaras