Hi all,
I just learned about closures in the Boost library and wanted to write some shorter code. To test, I used the following snippet:
#include <iostream> #include <string>
#include <boost/function.hpp> #include <boost/bind.hpp>
class FakeVisitor { public: virtual void visit(int e) {} virtual void visit(std::string e) = 0; };
template <typename T> class GenericVisitor : public FakeVisitor { public: boost::function<void (T e)> f; virtual void visit(T e) { f(e); } };
void print(int a, int i) { std::cout << a << ":" << i << std::endl; }
int main(int argc, char** argv) { GenericVisitor<int> v; v.f = boost::bind(&print, 0, _1);
v.visit((int) 1); v.visit(std::string("hallo")); //boost::bind(print, 1, _1)(2); }
Apparently, it does not work. Does anyone know why GenericVisitor<int> does not inherit visit(std::string)?
On Thu, 02 Sep 2010 12:15:27 +0200, Christoph wrote:
Hi all,
I just learned about closures in the Boost library and wanted to write some shorter code. To test, I used the following snippet:
#include <iostream> #include <string>
#include <boost/function.hpp> #include <boost/bind.hpp>
class FakeVisitor { public: virtual void visit(int e) {} virtual void visit(std::string e) = 0; };
template <typename T> class GenericVisitor : public FakeVisitor { public: boost::function<void (T e)> f; virtual void visit(T e) { f(e); } };
void print(int a, int i) { std::cout << a << ":" << i << std::endl; }
int main(int argc, char** argv) { GenericVisitor<int> v; v.f = boost::bind(&print, 0, _1);
v.visit((int) 1); v.visit(std::string("hallo")); //boost::bind(print, 1, _1)(2); }
Apparently, it does not work. Does anyone know why GenericVisitor<int> does not inherit visit(std::string)?
It does, but it's still declared a pure virtual function, i.e. FakeVisitor (and GenericVisitor, too) is an abstract class that cannot be instantiated. You would need to declare an implementation of the visit(std::string) virtual method in GenericVisitor to make it a non-abstract type.
Am Donnerstag, den 02.09.2010, 12:54 +0200 schrieb Michael Schwendt:
On Thu, 02 Sep 2010 12:15:27 +0200, Christoph wrote:
Hi all,
I just learned about closures in the Boost library and wanted to write some shorter code. To test, I used the following snippet:
#include <iostream> #include <string>
#include <boost/function.hpp> #include <boost/bind.hpp>
class FakeVisitor { public: virtual void visit(int e) {} virtual void visit(std::string e) = 0; };
template <typename T> class GenericVisitor : public FakeVisitor { public: boost::function<void (T e)> f; virtual void visit(T e) { f(e); } };
void print(int a, int i) { std::cout << a << ":" << i << std::endl; }
int main(int argc, char** argv) { GenericVisitor<int> v; v.f = boost::bind(&print, 0, _1);
v.visit((int) 1); v.visit(std::string("hallo")); //boost::bind(print, 1, _1)(2); }
Apparently, it does not work. Does anyone know why GenericVisitor<int> does not inherit visit(std::string)?
It does, but it's still declared a pure virtual function, i.e. FakeVisitor (and GenericVisitor, too) is an abstract class that cannot be instantiated. You would need to declare an implementation of the visit(std::string) virtual method in GenericVisitor to make it a non-abstract type.
Ah, sorry, wrong snippet. It does not work with
virtual void visit(std::string e) {}
in FakeVisitor.
The intention is to extend a visitor pattern with a templated class that overloads exactly one method.
On Thu, 02 Sep 2010 15:23:54 +0200, Christoph wrote:
Am Donnerstag, den 02.09.2010, 12:54 +0200 schrieb Michael Schwendt:
On Thu, 02 Sep 2010 12:15:27 +0200, Christoph wrote:
Hi all,
I just learned about closures in the Boost library and wanted to write some shorter code. To test, I used the following snippet:
#include <iostream> #include <string>
#include <boost/function.hpp> #include <boost/bind.hpp>
class FakeVisitor { public: virtual void visit(int e) {} virtual void visit(std::string e) = 0; };
template <typename T> class GenericVisitor : public FakeVisitor { public: boost::function<void (T e)> f; virtual void visit(T e) { f(e); } };
void print(int a, int i) { std::cout << a << ":" << i << std::endl; }
int main(int argc, char** argv) { GenericVisitor<int> v; v.f = boost::bind(&print, 0, _1);
v.visit((int) 1); v.visit(std::string("hallo")); //boost::bind(print, 1, _1)(2); }
Apparently, it does not work. Does anyone know why GenericVisitor<int> does not inherit visit(std::string)?
It does, but it's still declared a pure virtual function, i.e. FakeVisitor (and GenericVisitor, too) is an abstract class that cannot be instantiated. You would need to declare an implementation of the visit(std::string) virtual method in GenericVisitor to make it a non-abstract type.
Ah, sorry, wrong snippet. It does not work with
virtual void visit(std::string e) {}
in FakeVisitor.
The intention is to extend a visitor pattern with a templated class that overloads exactly one method.
v.FakeVisitor::visit(std::string("hallo"));
On Thu, 02 Sep 2010 15:23:54 +0200, Christoph wrote:
The intention is to extend a visitor pattern with a templated class that overloads exactly one method.
Since my reply sent yesterday is truncated for unknown reasons, let me try to reconstruct the missing part:
| v.FakeVisitor::visit(std::string("hallo"));
The declaration of GenericVisitor::visit hides the other visit members found in the base class FakeVisitor. You would need to pull it into scope either as above or with a "using" declaration (using FakeVisitor::visit) in GenericVisitor. That is independent from what you're trying to achieve with Boost, btw.