Is there anyway I can specialize JUST the Contains function of the class below?
I tried for hours and cannot figure it out as I'm not too good at specialization but I learned templates recently.
I want to do something like:
bool Contains(string ToFind)
{
//If typeof(T) == typeof(string) or typeID or typeName.. then execute something different..
for (unsigned I = 0; I < TypeData.size(); I++)
{
if (TypeData[I] == ToFind)
return true;
}
return false;
}
The Templated Class containing the "Contains" function..
#ifndef TEMPLATES_H_INCLUDED
#define TEMPLATES_H_INCLUDED
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class CustomType
{
private:
vector<T> TypeData;
void Insert() {}
template<typename A, typename... Args>
CustomType& Insert(A FirstArg, const Args... RemainingArgs)
{
this->TypeData.push_back(FirstArg);
Insert(RemainingArgs...);
return *this;
}
public:
CustomType() {}
template<typename A, typename... Args>
CustomType(A FirstArg, const Args... RemainingArgs)
{
this->TypeData.push_back(FirstArg);
Insert(RemainingArgs...);
}
~CustomType() {}
int size() {return TypeData.size();}
//Other functions/overloads removed at the moment.. just for this post.
bool Contains(T ToFind)
{
for (unsigned I = 0; I < TypeData.size(); I++)
{
if (TypeData[I] == ToFind)
return true;
}
return false;
}
};
#endif // TEMPLATES_H_INCLUDED