function object design problem

M

mati

Suppose there is some complex functor, and we are using some helper
functions inside:

class Func {
private:
inline void helper(...) {...}
public:
RetType operator()(const &data, Type1 param1, Type2 param2, ...)
{
const Type1 precomputed_param1 = some_heavy_math(param1, ...);
...
... //with excessive use of helper functions
}
};

However, this helper function needs a lots of parameters, some
precomputed in operator(), some given to it (ex. param1,param2).
Question is - how to pass data references/pointers and parameters to the
helper functions? With a remark, that helpers needs to be fast (inline
is a must).
Two ideas are:
1. Pass to function by reference/value.
2. Use private Func data members.

However, 1. yelds large argument list, and 2. can't deal with parameters
passed to operator() as references, also parameters can't be const
(which is a good thing).

I haven't come up with anything 100% nice and fast, so I'm asking here,
maybe there is a way.


Thanks
 
B

Barry

mati said:
Suppose there is some complex functor, and we are using some helper
functions inside:

are you gonna make it templated?
or you just forgot to type it?
class Func {
private:
inline void helper(...) {...}
public:
RetType operator()(const &data, Type1 param1, Type2 param2, ...)
{
const Type1 precomputed_param1 = some_heavy_math(param1, ...);
...
... //with excessive use of helper functions
}
};

However, this helper function needs a lots of parameters, some
precomputed in operator(), some given to it (ex. param1,param2).
Question is - how to pass data references/pointers and parameters to the\

pointer is not an issue.
on reference, you can use

template <class T>
class RefHolder
{
T& ref_;
public:
RefHolder(T& ref) : ref_(ref) {}
operator T& () const
{
return ref_;
}
};

template <class T>
inline RefHolder<T> ByRef(T& t)
{
return RefHolder<T>(t);
}

as wrapper

helper functions? With a remark, that helpers needs to be fast (inline
is a must).
Two ideas are:
1. Pass to function by reference/value.
2. Use private Func data members.

However, 1. yelds large argument list, and 2. can't deal with parameters
passed to operator() as references, also parameters can't be const

using RefHolder, the constness will be traited out.
 
J

James Kanze

Suppose there is some complex functor, and we are using some helper
functions inside:
class Func {
private:
inline void helper(...) {...}
public:
RetType operator()(const &data, Type1 param1, Type2 param2, ...)
{
const Type1 precomputed_param1 = some_heavy_math(param1, ...);
...
... //with excessive use of helper functions
}
};
However, this helper function needs a lots of parameters, some
precomputed in operator(), some given to it (ex. param1,param2).
Question is - how to pass data references/pointers and parameters to the
helper functions? With a remark, that helpers needs to be fast (inline
is a must).
Two ideas are:
1. Pass to function by reference/value.
2. Use private Func data members.
However, 1. yelds large argument list, and 2. can't deal with parameters
passed to operator() as references, also parameters can't be const
(which is a good thing).
I haven't come up with anything 100% nice and fast, so I'm asking here,
maybe there is a way.

This is a fairly frequent problem, not limited to functional
objects. What I usually use is a Context member struct:
something like:

class Func
{
private:
struct Context
{
// necessary data...
} ;
inline void helper( Context& cx )
{
// use cx...
}

public:
RetType operator()(
// ...
)
{
Context cx = { /* ... */ } ;
}
} ;

(And just a note in passing: it doesn't usually make sense for a
functional object to have more than one or two parameters. Any
more, and it probably can't be used in contexts where a
functional object would have advantages over a simple function.
But the above solution can also be used for normal functions.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top