fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4. #include <utility>
  5.  
  6. class ColumnBase {
  7. std::string m_name;
  8. public:
  9. ColumnBase(const std::string& name) : m_name(name) {}
  10. std::string name() { return m_name; }
  11. };
  12.  
  13. template<class T>
  14. class Column : public ColumnBase {
  15. std::vector<T> m_items;
  16. public:
  17. Column(const std::string& name) :ColumnBase(name) {}
  18. };
  19.  
  20. template<class... T>
  21. class Table {
  22. std::tuple<Column<T>...> m_columns;
  23.  
  24. template<std::size_t... index>
  25. std::vector<ColumnBase*> columns_vec_helper(std::index_sequence<index...>) {
  26. return { (&std::get<index>(m_columns))... };
  27. }
  28.  
  29. public:
  30. Table(Column<T>&&... columns) : m_columns(std::move(columns)...) {}
  31.  
  32. std::vector<ColumnBase*> columns_vec() {
  33. return columns_vec_helper(std::make_index_sequence<sizeof...(T)>{});
  34. }
  35. };
  36.  
  37.  
  38. int main() {
  39. Table<std::size_t, std::string, int, int> t ({"id"}, {"name"}, {"x"}, {"y"});
  40. for (const auto& colBase : t.columns_vec()) std::cout << "column " << colBase->name() << "\n";
  41. return 0;
  42. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
column id
column name
column x
column y