fork download
  1. #include <iostream>
  2.  
  3. class List
  4. {
  5. public:
  6. constexpr List():
  7. m_data(nullptr),
  8. m_next(this),
  9. m_previous(this)
  10. {
  11. }
  12.  
  13. List(const void *data):
  14. m_data(data)
  15. {
  16. m_next = &root;
  17. m_previous = root.m_previous;
  18. root.m_previous = root.m_previous->m_next = this;
  19. }
  20.  
  21. template <typename Func>
  22. static void ForEach(Func f)
  23. {
  24. List *ptr = root.m_next;
  25. while (ptr != &root)
  26. {
  27. f(ptr->m_data);
  28. ptr = ptr->m_next;
  29. }
  30. }
  31. private:
  32. static List root;
  33. private:
  34. const void *m_data;
  35. List *m_next;
  36. List *m_previous;
  37. };
  38.  
  39.  
  40. List a("foo");
  41. List b("bar");
  42. List List::root;
  43.  
  44. int main()
  45. {
  46. List::ForEach([](const void* ptr)
  47. {
  48. std::cout << static_cast<const char*>(ptr) << std::endl;
  49. });
  50. }
Runtime error #stdin #stdout 0s 3336KB
stdin
Standard input is empty
stdout
Standard output is empty