Search code examples
c++templatesgeneric-programming

template definitions syntax


Short question, are these definitions same?

1.

    template<class T1>
    template<class T2>
    void function(T1 *a, T2*b);

2.

    template<class T1,class T2>
    void function(T1 *a, T2*b);

Solution

  • No they are not the same. The first case won't compile as a free function(live example). Normally you would do the first case when you have a template class and you have a function in it that takes another template parameter

    template <typename T1>
    class Foo
    {
    public:
        template<typename T2>
        void function(T1 *a, T2*b);
    };
    
    template<class T1>
    template<class T2>
    void Foo<T1>::function(T1 *a, T2*b);
    

    Your second example is just fine as a function with two template parameters.