|
Boost Users : |
Subject: Re: [Boost-users] [partly: Boost.Function] typesafe (templated) wrapper around class with void* API
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-01-29 11:34:08
AMDG
Christoph Duelli wrote:
> template<typename T>
> class B : public A
> {
> public:
> void do_something(T* t) { A::do_something(t); }
> protected:
> // Force user to provide a correctly typed callback.
> virtual bool help_me(const T*) =0;
> private:
> // call the user's safe callback
> bool help_me(const void* t)
> {
> return help_me(static_cast<const T*>(t));
> }
> };
>
> More specific questions:
> i) I assume the penalty for my callback "help_me" is one
> more indirection. Can't see that optimized away.
>
Use CRTP
template<class Derived, class T>
class B {
private:
bool help_me(const void* t) {
return static_cast<Derived*>(this)->help_me(static_cast<const
T*>(t));
}
};
> ii) Assuming A's interface gets passed a real callback
> function (a Boost.Function) instead:
>
> class A
> {
> public:
> typedef boost::function<bool(const void*)> helper_func;
> void do_something(void*, helper_func);
> };
>
> is it somehow possible to get a typesafe templated B such that
> template<typename T>
> class B : public A
> {
> public:
> typedef boost::function<bool(const T*)> helper_func;
> void do_something(T* t, helper_func hf)
> {
> // this makes my compiler unhappy (when instanced).
> // I do understand that... can it be worked around somehow?
> A::do_something(t, hf);
> }
> };
>
A::do_something(t, boost::lambda::bind(hf,
boost::lambda::ll_static_cast<const T*>(boost::lambda::_1)));
In Christ,
Steven Watanabe
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