Reputation: 431
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?
Upvotes: 26
Views: 2209
Reputation: 170055
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.
#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;
}
Upvotes: 25
Reputation: 13073
It looks that you are using dlsym
/ GetProcAddress
to get the functions address.
In this case, you need at least 2 call sites to disambiguate the call, as the CPU actually does different things for each of these calls.
enum ReturnType { rtInt, rtDouble };
void SetPtrToFunction( std::string fName , enum ReturnType typeOfReturn );
struct Function {
enum ReturnType rt;
union {
std::function< int(double) > mIntFunction;
std::function< double(double) > mDoubleFunction;
} u;
} mFunction;
So the function would need to be instantiated with a known return type, and then this used with some tagged union to get the correct function call.
int A::doCall( double value ) {
if( mFunction.rt == rtInt ) {
int result = mFunction.mIntFunction( value );
} else if( mFunction.rt == rtDouble ) {
double result = mFunction.mDoubleFunction( value );
}
}
Upvotes: 1
Reputation: 73366
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);
};
Upvotes: 1