Rajeshwar
Rajeshwar

Reputation: 11651

What is wrong with this template partial specialization code?

I am using the following code :

template<typename t>
struct foo
{
    foo(t var)
    {
        std::cout << "Regular Template";
    }
};



template<typename t>
struct foo<t*>
{
    foo(t var)
    {
        std::cout << "partially specialized Template " << t;
    }
};


int main()
{
    int* a = new int(12);

    foo<int*> f(a); //Since its a ptr type it should call specialized template

}

However I am getting the error

Error   1   error C2664: 'foo<t>::foo(int)' : cannot convert parameter 1 from 'int *' to 'int'  

Upvotes: 0

Views: 40

Answers (1)

Baum mit Augen
Baum mit Augen

Reputation: 50043

The constructor of the both templates takes a t by value, which in your example is int, not int*. To make this compile use

template<typename t>
struct foo<t*>
{
    foo(t* var)
    {
        std::cout << "partially specialized Template " << var;
    }
};

if that fits your logic, else pass an int to the constructor. (Live)

Upvotes: 3

Related Questions