Re: How to specially treat the beginning of a type list?
* PengYu.UT@gmail.com:
On Jul 28, 1:13 pm, "Thomas J. Gritzan" <Phygon_ANTIS...@gmx.de>
template <typename Base, typename T>
class visitor : public Base {
public :
virtual void visit(T &) = 0 ;
// using Base::visit; // not needed here.
};
template <typename Base, typename T1, typename T>
class visitor<visitor<Base, T1>,T> : public visitor<Base,T1> {
public :
virtual void visit(T &) = 0 ;
using visitor<Base,T1>::visit;
};
HTH (especially since I don't know boost.mpl)
That is exact what I meant. I need the ability to tell the difference
between the list head and other elements of the list. For the head of
the list, there is no need to "using visit", because there is nothing
to use. For the subsequent elements, I need to "using visit" get the
"visit" function from the base class into the derived class.
I think perhaps the Real Problem(TM) may be something else.
If the problem is how to get the "everybody can visit each other"
working, try
#include <iostream>
#include <ostream>
void say( char const s[] ) { std::cout << s << std::endl; }
class A;
class B;
class IVisitor
{
public:
virtual ~IVisitor() {}
virtual void visit( A& ) = 0;
virtual void visit( B& ) = 0;
};
class IAcceptor
{
public:
virtual ~IAcceptor() {}
virtual void accept( IVisitor& v ) = 0;
};
class ISocial: public virtual IVisitor, public virtual IAcceptor
{};
class A: public ISocial
{
public :
void visit( A& ) { say( "A-A"); }
void visit( B& ) { say( "A-B" ); }
void accept( IVisitor& v ) { v.visit( *this ); }
};
class B: public ISocial
{
public :
void visit( A& ) { say( "B-A" ); }
void visit( B& ) { say( "B-B" ); }
void accept( IVisitor& v) { v.visit(*this); }
};
int main()
{
A a;
B b;
ISocial& ra = a ;
ISocial& rb = b ;
ra.accept( ra ); // A-A
rb.accept( ra ); // A-B
ra.accept( rb ); // B-A
rb.accept( rb ); // B-B
}
No need to bring in type list machinery etc., casting etc. (note: the
purpose of the visitor pattern is to /centralize/ any downcasting!);
that's over-engineering.
But I think the idea of everybody visiting each other is perhaps not
very sound, itself possibly a case of over-engineering or over-abstraction.
Instead, perhaps the needs of each particular class indicate some more
limited application of the visitor pattern.
Cheers,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?