有没有办法在类之外访问模板参数?

-1 c++ templates

所以我试图创建一个访问该类的模板参数所需的函数,但我不想将该函数放在类本身中。
这是一个例子:

  template <class Type>
  class Node
  {
      public:
          Type Value;
          Type* ptr_next;
    
  };

  void element_output(Node<  /*template argument of the class*/>  * head)
  {
      while(head!=NULL)
      {
          std::cout << head->Value << "\n";
          head = head->ptr_next;
        
      }
  }
Run Code Online (Sandbox Code Playgroud)

我尝试使用该功能的模板,但没有成功。我正在考虑用所有类型重载函数参数中的 Node 模板参数。但我知道一定会发生错误。

所以我认为可以工作的是模板参数的 getter 和 setter,类似于构造函数和私有成员。

Jar*_*d42 6

制作函数模板:

template <typename T>
void element_output(Node<T>* head)
{
    while (head != nullptr)
    {
        std::cout << head->Value << "\n";
        head = head->ptr_next;
    }
}
Run Code Online (Sandbox Code Playgroud)