How to loop the template arguments?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class POne
{
  public :
    static void action() {std::cout<<"POne\n";}
};

class PTwo
{
  public :
    static void action() {std::cout<<"PTwo\n";}
};

class PThree
{
  public :
    static void action() {std::cout<<"PThree\n";}
};

template<typename policyOne, typename policyTwo>
class test
{
  public :
    void run();
};

template<typename policyOne, typename policyTwo>
void test<policyOne, policyTwo>::run()
{
  policyOne::action();
  policyTwo::action();
}

int main()
{ 
  test<POne, PTwo> A;
  A.run();

  std::cout<<std::endl<<"system pause";
  std::cin.get();
  return 0;
}


How could I make it work like
1
2
3
4
5
6
7
8
9
10
11
int main()
{ 
  //some kind of loop
  test<POne~PThree, POne~PThree> A;
  A.run();
  //some kind of loop

  std::cout<<std::endl<<"system pause";
  std::cin.get();
  return 0;
}

so it would generate 9 different compositions of the policies?
Thank you very much
Not possible in a "automated" manner, I would say. You'll have to type all 9 permutations by hand.

Remember that a template is nothing but coder's candy. The following 2 are the exact same thing, and I wouldn't be surprised if they generated the exact same executable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using std::cout;
using std::endl;

template<class T>
class MyTClass
{
public:
    T a;
};

int main()
{
    MyTClass<int> O1;
    O1.a = 1;
    MyTClass<double> O2;
    O2 = 2.0;
    cout << O1.a << " | " << O2.a << endl;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using std::cout;
using std::endl;

class MyTClassInt
{
public:
    int a;
};

class MyTClassDouble
{
public:
    double a;
};


int main()
{
    MyTClassInt O1;
    O1.a = 1;
    MyTClassDouble O2;
    O2 = 2.0;
    cout << O1.a << " | " << O2.a << endl;
}

One thing you can do is write a separate program to generate the code you need.

Another thing you can do is use some boost preprocessor magic...
( http://www.boost.org/doc/libs/1_46_1/libs/preprocessor/doc/ )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/cat.hpp>
#include <iostream>

#define PC(id,msg) struct P##id { static void Action() { std::cout << #msg << "\n"; } };

PC(1,POne) PC(2,PTwo) PC(3,PThree) PC(4,PFour) PC(5,PFive) PC(6,PSix) PC(7,PSeven)

template<class PL, class PR> struct Test { void Run() { PL::Action(); PR::Action(); } };

#define INNER_LOOP(z,rightPn,leftPn) \
    Test< P##leftPn , BOOST_PP_CAT(P,BOOST_PP_INC(rightPn)) > ().Run(); std::cout << "\n";

#define OUTER_LOOP(z,leftPn,unused) BOOST_PP_REPEAT(7,INNER_LOOP,BOOST_PP_INC(leftPn)) \
    std::cout << "(hit enter to continue...)\n\n"; std::cin.get();

#define DO_IT_AND_WHEN_YOU_RE_DONE std::cout << "\n"; BOOST_PP_REPEAT(7,OUTER_LOOP,~)

int main() { DO_IT_AND_WHEN_YOU_RE_DONE return 0; }
Last edited on
Thanks a lot

One thing you can do is write a separate program to generate the code you need.

That is what I planned at first

But your suggestion--boost preprocessor magic looks more attractive for me
although I only know a little bit about preprocessor.I would give it a try
Last edited on
Topic archived. No new replies allowed.