Hi,
The line with the comment "// redundant" is redundant in the sense
that is never used in the main program. However, removing that line
will invalidate the program. I'm wondering if there is any way to
intelligently remove that line while still leaving the program
compilable.
Thanks,
Peng
#include <iostream>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/placeholders.hpp>
class base {
public :
virtual ~base() {}
virtual void accept(base& b) = 0 ;
void visit() { } // redundant
};
template <typename Base, typename T>
class visitor : public Base {
public :
virtual void visit(T &) = 0 ;
using Base::visit;
};
class A;
class B;
typedef boost::mpl::vector<A, B> AB;
typedef boost::mpl::fold<AB, base,
visitor<boost::mpl::placeholders::_1, boost::mpl::placeholders::_2>
::type v;
class A : public v {
public :
void visit(A&) {
std::cout << "A-A" << std::endl;
}
void visit(B&) {
std::cout << "A-B" << std::endl;
}
void accept(base& b) {
static_cast<v&>(b).visit(*this);
}
};
class B : public v {
public :
void visit(A&) {
std::cout << "B-A" << std::endl;
}
void visit(B&) {
std::cout << "B-B" << std::endl;
}
void accept(base& b) {
static_cast<v&>(b).visit(*this);
}
};
int main(){
A a;
B b;
base& x1 = a ;
base& x2 = b ;
x1.accept(x1); // A-A
x2.accept(x1); // A-B
x1.accept(x2); // B-A
x2.accept(x2); // B-B
return EXIT_SUCCESS;
}
You forgot to mark which line was redundant. Which line is it?