How to convert a vector of base classes to template based code?

Hi, The following program uses virtual functions. Essentially I need to go through all the pairs in 'v'. Since I have already know what are in v at compile time. I would like to convert the dynamic polymorphism to template. Therefore, the runtime would be better. I think this should be done with the help of boost::mpl. But I'm not sure how to do it. Would you please help me? Thanks, Peng #include <iostream> #include <vector> #include <boost/shared_ptr.hpp> struct shape { virtual void show_me() const = 0; virtual ~shape() { } }; struct square : public shape { void show_me() const { std::cout << "square"; } }; struct circle : public shape { void show_me() const { std::cout << "circle"; } }; struct triangle : public shape { void show_me() const { std::cout << "triangle"; } }; int main() { std::vector<boost::shared_ptr<shape> > v; { boost::shared_ptr<shape> s(new square); v.push_back(s); } { boost::shared_ptr<shape> s(new circle); v.push_back(s); } { boost::shared_ptr<shape> s(new square); v.push_back(s); } { boost::shared_ptr<shape> s(new triangle); v.push_back(s); } for(std::vector<boost::shared_ptr<shape> >::const_iterator it = v.begin(); it != v.end(); ++ it) { for(std::vector<boost::shared_ptr<shape> >::const_iterator it2 = it + 1; it2 != v.end(); ++ it2) { (*it)->show_me(); std::cout << " vs. "; (*it2)->show_me(); std::cout << std::endl; } } }

Peng Yu <pengyu.ut <at> gmail.com> writes:
The following program uses virtual functions. Essentially I need to go
[skip]
}
Am I correctly understand that you are looking for something link this: =================================================== #include <iostream> #include <boost/fusion/algorithm.hpp> #include <boost/fusion/container.hpp> struct square { void show_me() const { std::cout << "square"; } }; struct circle { void show_me() const { std::cout << "circle"; } }; struct triangle { void show_me() const { std::cout << "triangle"; } }; template<class T, class End> void out_pair(const T &, const End &, const End &) {} template<class T, class Begin, class End> void out_pair(const T & t, const Begin & begin, const End & end) { t.show_me(); std::cout << " vs. "; deref(begin).show_me(); std::cout << std::endl; out_pair(t, next(begin), end); } template<class End> void out(const End &, const End &) {} template<class Begin, class End> void out(const Begin & begin, const End & end) { out_pair(deref(begin), next(begin), end); out(next(begin), end); } int main() { typedef boost::fusion::vector<square, circle, square, triangle> Vec; Vec v; out(begin(v), end(v)); } =================================================== Best Regards, Sergei

On Thu, Jul 10, 2008 at 2:47 AM, Sergei Politov <spolitov@gmail.com> wrote:
Peng Yu <pengyu.ut <at> gmail.com> writes:
The following program uses virtual functions. Essentially I need to go
[skip]
}
Am I correctly understand that you are looking for something link this: =================================================== #include <iostream>
#include <boost/fusion/algorithm.hpp> #include <boost/fusion/container.hpp>
struct square { void show_me() const { std::cout << "square"; } };
struct circle { void show_me() const { std::cout << "circle"; } };
struct triangle { void show_me() const { std::cout << "triangle"; } };
template<class T, class End> void out_pair(const T &, const End &, const End &) {}
template<class T, class Begin, class End> void out_pair(const T & t, const Begin & begin, const End & end) { t.show_me(); std::cout << " vs. "; deref(begin).show_me(); std::cout << std::endl; out_pair(t, next(begin), end); }
template<class End> void out(const End &, const End &) {}
template<class Begin, class End> void out(const Begin & begin, const End & end) { out_pair(deref(begin), next(begin), end); out(next(begin), end); }
int main() { typedef boost::fusion::vector<square, circle, square, triangle> Vec; Vec v; out(begin(v), end(v)); } ===================================================
Best Regards, Sergei
Hi, You understood me correctly. But it seems the transform from my code to your code is a little complicated. Is there any more direct analog between boost::fusion and the runtime algorithm. In particular, how to directly use two iterations with the second iteration start with something like it + 1 in my original code? Thanks, Peng

on Fri Jul 11 2008, "Peng Yu" <pengyu.ut-AT-gmail.com> wrote:
On Thu, Jul 10, 2008 at 2:47 AM, Sergei Politov <spolitov@gmail.com> wrote:
You understood me correctly. But it seems the transform from my code to your code is a little complicated. Is there any more direct analog between boost::fusion and the runtime algorithm. In particular, how to directly use two iterations with the second iteration start with something like it + 1 in my original code?
If you want something more direct, you may need to stick with the runtime world. What is your goal in using fusion? If it isn't because you need to run faster, you might look at http://stlab.adobe.com/wiki/images/c/c9/Boost_poly.pdf, which could give you some ideas for removing the coding burden of runtime polymorphism. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

On Fri, Jul 11, 2008 at 10:13 AM, David Abrahams <dave@boostpro.com> wrote:
on Fri Jul 11 2008, "Peng Yu" <pengyu.ut-AT-gmail.com> wrote:
On Thu, Jul 10, 2008 at 2:47 AM, Sergei Politov <spolitov@gmail.com> wrote:
You understood me correctly. But it seems the transform from my code to your code is a little complicated. Is there any more direct analog between boost::fusion and the runtime algorithm. In particular, how to directly use two iterations with the second iteration start with something like it + 1 in my original code?
If you want something more direct, you may need to stick with the runtime world.
What is your goal in using fusion? If it isn't because you need to run faster, you might look at http://stlab.adobe.com/wiki/images/c/c9/Boost_poly.pdf, which could give you some ideas for removing the coding burden of runtime polymorphism.
I do want it fast. The application is double dispatch. Using static polymorphism instead of dynamic polymorphism should give at least a few time runtime improvement. Do you mean that it is not possible to write the code in fusion as easy to read as my original code? I didn't quite catch what the key point in the slides that you pointed. Would you please let me know what is about and how it could apply to my problem? Thanks, Peng

on Fri Jul 11 2008, "Peng Yu" <pengyu.ut-AT-gmail.com> wrote:
On Fri, Jul 11, 2008 at 10:13 AM, David Abrahams <dave@boostpro.com> wrote:
What is your goal in using fusion? If it isn't because you need to run faster, you might look at http://stlab.adobe.com/wiki/images/c/c9/Boost_poly.pdf, which could give you some ideas for removing the coding burden of runtime polymorphism.
I do want it fast. The application is double dispatch. Using static polymorphism instead of dynamic polymorphism should give at least a few time runtime improvement.
It certainly could.
Do you mean that it is not possible to write the code in fusion as easy to read as my original code?
What I mean is that Fusion code will only be as easy to read as STL code when you're as familiar with Fusion as you are with STL today.
I didn't quite catch what the key point in the slides that you pointed. Would you please let me know what is about and how it could apply to my problem?
It's about type erasure but it seems to me from what you say about your requirements above that it won't help you very much, if at all. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
participants (3)
-
David Abrahams
-
Peng Yu
-
Sergei Politov