Re: How to resolve template dependencies with a generic callback passed *this?
On Jan 30, 3:42 am, Jesse Perla <jessepe...@gmail.com> wrote:
On Jan 29, 5:24 pm, Mathias Gaunard <loufo...@gmail.com> wrote:
Instead of writing
template<typename T>
A<T> make_a(T t)
Thanks Mathias,
But I think I am missing how this helps the interdependency of my
templates. I can't see how to use my class as written due to the T?
i.e. Unless I misunderstand, make_a(thing) would let the compiler
correctly deduce the type, but without the "auto" keyword there is no
shortcut to knowing the instantiated type of A.
A a = make_a(thing); //But A is a template, not an instantiated type?
My hunch is that the answer may have something to do with template
template arguments, but I can't figure it out.
You can break the circular dependancy with a template template
parameter like so:
template <template <typename> class Callback>
class A {
Callback <A> f_;
public:
A (Callback <A> f) : f_ (f) {}
void call () {f_ (*this);}
};
template <class T>
struct My_callback
{
void operator () (const T&t) {}
};
void foo ()
{
typedef A <My_callback> Amy;
My_callback <Amy> c;
Amy a (c);
a.call ();
}
or you could go with your instinct and use tr1::function to do type
erasure like so:
#include <tr1/functional>
namespace to {
class A {
std::tr1::function <void (const A&)> f_;
public:
template <class Callback>
A (Callback f) : f_ (f) {}
void call () {f_ (*this);}
};
template <class T>
struct My_callback
{
void operator () (const T&t) {}
};
void test ()
{
My_callback <A> c;
A a (c);
a.call ();
}
Regards,
Vidar Hasfjord
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]