|
Boost Users : |
From: John Christopher (jcxxr_at_[hidden])
Date: 2006-03-16 20:48:22
Hi;
The program belows compiles and works but I'd like to simplify it, maybe
using MPL. I spent some fascinating time educating myself on MPL, reading
C++ Template Metaprogramming, but I am still at sea... to say the least.
Basically the Functor contains a switch statement that is not convenient
especially as the number of T classes will increase dramatically.
Many thanks for any help on that
JCR
#include <iostream>
#include <vector>
#include <boost/mpl/vector.hpp>
struct Tbase
{
Tbase() { std::cout << "Tbase" << std::endl; }
};
struct T0 : public Tbase
{
T0() { std::cout << "T0" << std::endl; }
};
struct T1 : public Tbase
{
T1() { std::cout << "T1" << std::endl; }
};
typedef boost::mpl::vector<T0, T1> s;
std::vector<Tbase*> vec;
struct Functor
{
void operator()(const int& i)
{
switch(i)
{
case 0:
vec.push_back(new T0);
break;
case 1:
vec.push_back(new T1);
break;
}
// I'd like to simplify the above swtich statement by writing something
like:
vec.push_back(new boost::mpl::at<s,i>::type);
// but it does not compile and MinGW returns:
// error: i cannot appear in a constant expression
// error: template argument 2 is invalid.
}
};
int main (int argc, char ** argv)
{
std::vector<int> runtimeVec;
runtimeVec.push_back(0);
runtimeVec.push_back(1);
runtimeVec.push_back(0);
std::for_each(runtimeVec.begin(), runtimeVec.end(), Functor());
std::cout << vec.size() << std::endl;
return 0;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net