fork download
  1. #include <stdio.h>
  2. #include <stddef.h>
  3.  
  4. template <typename T> struct list_node_base
  5. {
  6. T *next;
  7. T *prev;
  8. };
  9.  
  10. template <typename T>
  11. struct linked_list_node
  12. {
  13. list_node_base<T> list_node;
  14. };
  15.  
  16. template <typename T, linked_list_node<T> T::*NODE, bool is_member>
  17. struct list_base
  18. {
  19. template <typename A, typename B>
  20. size_t offset_of(B A::*M)
  21. {
  22. return reinterpret_cast<size_t>(&(((T*)0)->*M));
  23. };
  24. };
  25.  
  26. template <typename T, linked_list_node<T> T::*NODE>
  27. struct list_base<T, NODE, true> : linked_list_node<T>
  28. {
  29. size_t offset()
  30. {
  31. return offset_of(&T::*NODE);
  32. }
  33. };
  34.  
  35. template <typename T, linked_list_node<T> T::*NODE>
  36. struct list_base<T, NODE, false> : linked_list_node<T>
  37. {
  38. size_t offset()
  39. {
  40. return offset_of(&T::list_node);
  41. }
  42. };
  43.  
  44. template <typename T, linked_list_node<T> T::*NODE = nullptr>
  45. struct linked_list : list_base<T, NODE, (linked_list_node<T> T::*)nullptr != NODE>
  46. //struct linked_list : list_base<T, NODE, NODE>
  47. {
  48. };
  49.  
  50. struct foo : linked_list_node<foo>
  51. {
  52. };
  53.  
  54. struct bar
  55. {
  56. linked_list_node<bar> node;
  57. };
  58.  
  59. linked_list<foo> foo_list;
  60. linked_list<bar, &bar::node> bar_list;
  61.  
  62. int main(int, char **)
  63. {
  64. return 0;
  65. }
Compilation error #stdin compilation error #stdout 0s 3292KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘size_t list_base<T, NODE, true>::offset()’:
prog.cpp:31:24: error: expected unqualified-id before ‘*’ token
   return offset_of(&T::*NODE);
                        ^
stdout
Standard output is empty