fork download
  1. #include <utility>
  2.  
  3. // empty declaration with variable number of arguments
  4. template<typename...>
  5. struct hnode;
  6.  
  7. // specialization for 1 template argument
  8. template<typename T>
  9. struct hnode<T> {
  10. T data;
  11. std::nullptr_t next;
  12. };
  13.  
  14. // specialization for multiple template arguments
  15. template<typename T, typename... Rest>
  16. struct hnode<T, Rest...> {
  17. T data;
  18. hnode<Rest...>* next;
  19. };
  20.  
  21. template<typename T>
  22. hnode<T> hcons(T&& val, std::nullptr_t) {
  23. return { std::forward<T>(val), nullptr };
  24. }
  25.  
  26. template<typename T, typename... Rest>
  27. hnode<T, Rest...> hcons(T&& val, hnode<Rest...>& next) {
  28. return { std::forward<T>(val), &next };
  29. }
  30.  
  31. int main() {
  32. hnode<int> three = hcons(1, nullptr);
  33. auto two = hcons("hi", three);
  34. }
Success #stdin #stdout 0s 3336KB
stdin
Standard input is empty
stdout
Standard output is empty