About variadic templates
- by chedi
Hi,
I'm currently experiencing with the new c++0x variadic templates, and it's quit fun, Although I have a question about the process of member instanciation.
in this example, I'm trying to emulate the strongly typed enum with the possibility of choose a random valid strong enum (this is used for unit testing).
#include<vector>
#include<iostream>
using namespace std;
template<unsigned... values> struct sp_enum;
/*
 this is the solution I found, declaring a globar var
 vector<unsigned> _data;
 and it work just fine
*/
template<> struct sp_enum<>{
  static const unsigned _count  = 0;
  static vector<unsigned> _data;
};
vector<unsigned> sp_enum<>::_data;
template<unsigned T, unsigned... values>
struct sp_enum<T, values...> : private sp_enum<values...>{
  static const unsigned _count = sp_enum<values...>::_count+1;
  static vector<unsigned> _data;
  sp_enum(                       ) : sp_enum<values...>(values...) {_data.push_back(T);}
  sp_enum(unsigned v             )                                 {_data.push_back(v);}
  sp_enum(unsigned v, unsigned...) : sp_enum<values...>(values...) {_data.push_back(v);}
};
template<unsigned T, unsigned... values> vector<unsigned> sp_enum<T, values...>::_data;
int main(){
  enum class t:unsigned{Default = 5, t1, t2};
  sp_enum<t::Default, t::t1, t::t2> test;
  cout <<test._count << endl << test._data.size() << endl;  
  for(auto i= test._data.rbegin();i != test._data.rend();++i){cout<< *i<< ":";}
}
the result I'm getting with this code is :
3
1
5:
can someone point me what I'm messing here ???
Ps: using gcc 4.4.3