I am trying to create a template function that is overloaded for pointer and non pointers. I did this and it works.
template<class D, class V>
bool Same(D d, V v){ return d == v; }
template<class D, class V>
bool Same(D* d, V v) { return *d==v;}
Now I want to extend it such that a templated container is a paramater and there must be one version for the container with pointer and other with the container for non pointers. I am not able to figure it out. I tried this but it won't work.
template< template<class> class Container, class Data, class Value>
bool func(Container<Data> &c, Value v)
{
return c[0] == v;
}
template< template<class> class Container, class Data, class Value>
bool func(Container<Data*> &c, Value v)
{
return *c[0] == v;
}
The error c2040 says int* differs in level of indirection from int and points to the first function.
How can I get it to work?
Rest of thecode
template<class D>
class Vec
{
std::vector<D> m_vec;
public:
void push_back(D d) { m_vec.push_back(d); }
D operator[](int i) { return m_vec[i]; }
};
void test_template()
{
Same<int, int>(2,3);
Info i = {4};
Same<Info, int>(i, 2);
Info ii = {2 };
Info *pi = ⅈ
Same<Info, int>(pi, 2);
Vec<int> iv;
iv.push_back(3);
func<Vec, int, int>(iv, 3);
Vec<int*> pv;
pv.push_back(new int(3));
func<Vec, int*, int>(pv, 3);
}