Templates in C ++

can we declare a function templatein a normal class without a template class, or should it always be inside template class?

+3
source share
3 answers

can declare a template function in a normal class with a template class

Yes we can. for instance

class demo
{
   public:
   template <typename T>
   void func(const T& x) { 
      //do stuffs 
   }
};

int main()
{
   demo d;
   d.func<int>(5);
}

works great

+2
source

Yes, you can have template functions in non-template classes, for example:

struct X {
    template<class T>
    void f(const T& t) {
        // ...
    }
};
+2
source

, , , . , , -

0

Source: https://habr.com/ru/post/1774887/


All Articles