|
Boost Users : |
From: Russell L. Carter (rcarter_at_[hidden])
Date: 2008-01-23 10:18:13
dizzy wrote:
> I dont understand what you want to achieve so I can't recommend something
> based on that but strictly speaking about the issue, fooFO receives a
> template template parameter and not a type so you cannot give it an
> instantiation of MHBase, you probably want to give it MHBase. So:
>
> FooFunc ff = fooFO<MHBase>(*this);
>
> (even tho I don't understand why in that case fooFO is made to receive a
> template template parameter being defined inside MHBase, you do not need the
> flexibility of being able to give it something else than MHBase do you?)
>
Hi Dizzy,
What I'm trying to do is generically package up a function object to
the MHBase's member foo(). I can certainly do this directly
for every derived class's member foo_() but I thought I would try to
see if it could be done once in the MHBase. How do people
accomplish this?
Looking at it again with your comment in mind, the fooFO doesn't need to
be a template (I think) so can be even more simplified, but then I
get "void value not ignored as it ought to be" at the line:
"FooFunc ff = put.getFooFunc();"
I get the same thing if I try: FooFunc ff = MHBase<Put>::fooFO(put);
Here's the code:
#include <boost/function.hpp>
#include <iostream>
class MHBAccessor {
template<class> friend class MHBase;
template<class Derived>
static int foo(Derived& derived, int i) { return derived.foo_(i); }
};
typedef boost::function<int (int)> FooFunc;
template <class Derived>
struct MHBase {
Derived& derived() { return static_cast<Derived&>(*this); }
int foo(int i) { return MHBAccessor::foo(this->derived(), i); }
struct fooFO {
fooFO(MHBase<Derived>& mhb) : mhb_(mhb) {}
void operator()(int i) { mhb_.foo(i); }
private:
MHBase<Derived>& mhb_;
};
FooFunc getFooFunc() {
FooFunc ff = fooFO(*this);
return ff;
}
};
struct Put : public MHBase<Put>
{
private:
friend class MHBAccessor;
int foo_(int i) { return i; }
};
int main(int argc, char **argv)
{
Put put;
std::cout << put.foo(123) << std::endl;
FooFunc ff = put.getFooFunc();
// FooFunc ff = MHBase<Put>::fooFO(put);
std::cout << ff(456) << std::endl;
return 0;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net