Gitmo
Gitmo

Reputation: 2424

Define Function Outside of template class with both type and non-type parameters

template <typename T, int a, UINT32 B>
class Test
{
    public:
        Test(T, int);
        void foo();
        int bar();
};

How do I define the constructor and functions outside of this class?

Upvotes: 1

Views: 992

Answers (2)

Alexander Putilin
Alexander Putilin

Reputation: 2342

Just include full template "specification" before constructor/method definition, and also include template parameters names in angle brackets after class name when qualifying methods/constructors names.

Like this:

#include <iostream> 
#include <vector>

template <typename T, int a, int b>
class Test
{
    public:
        Test(T t, int i);
        void foo();
        int bar();
};

template <typename T, int a, int b>
Test<T, a, b>::Test(T t, int i)
{
    std::cout << "Constructor, i = " << i << std::endl;
}

template <typename T, int a, int b>
void Test<T, a, b>::foo()
{
    std::cout << "foo() Template params:" << a << " " << b << std::endl;
}

template <typename T, int a, int b>
int Test<T, a, b>::bar()
{
    std::cout << "bar() Template params:" << a << " " << b << std::endl;
}

int main()
{
    Test<std::vector<double>, 13, 42> t(std::vector<double>(2), 5);
    t.foo();
    t.bar();
}

Upvotes: 2

Caesar
Caesar

Reputation: 9841

template <typename T, int a, int B>
Test<T, a, B>::Test(T x1, int x2)
{

}

The same way can be done for the function.

Upvotes: 1

Related Questions