fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <type_traits>
  4. #include <memory>
  5. using namespace std;
  6.  
  7.  
  8.  
  9. struct Slave {
  10. virtual char const* Type() = 0;
  11. };
  12.  
  13. template<typename T>
  14. struct Slave_T : public Slave{
  15. typedef T type;
  16. virtual char const* Type() {
  17. return typeid( T ).name();
  18. }
  19. };
  20.  
  21. template <typename ...T>
  22. struct Master {
  23. Master()
  24. {
  25. MakeSlave<T...,_my_centinel_type>();
  26. cout << "All Slaves:" << endl;
  27. for (auto const& slave : v){
  28. cout << slave ->Type() << endl;
  29. }
  30. }
  31. private:
  32. struct _my_centinel_type {};
  33.  
  34. template<typename U, typename ...Rest>
  35. typename std::enable_if<!std::is_same<U,_my_centinel_type>::value , void>::type MakeSlave()
  36. {
  37. v.push_back( std::make_shared<Slave_T<U>>());
  38. MakeSlave<Rest...>();
  39. }
  40.  
  41. template<typename U>
  42. typename std::enable_if<std::is_same<U,_my_centinel_type>::value , void>::type MakeSlave(){
  43.  
  44. }
  45.  
  46. vector<shared_ptr<Slave>> v;
  47. };
  48.  
  49. int main()
  50. {
  51. Master<int, double, char> m;
  52. //Print("Hello", '!', 123, 123);
  53. return 0;
  54. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
All Slaves:
i
d
c