I am trying to create a class thats going to draw elements from a set of vectors (and also hold these vectors as containers inside the class), but i feel that when managing the vector having lots of functions like vectorOneAdd, vectorTwoAdd used in order to add elements to the vector is pointless. There must be a better way, thats why i am asking here, I heard you can use templates to do it, but i am not quite certain how. Assistance needed. Don't want to have lots of pointless code in.
Example of what I mean below:
class Cookie
{
std::vector<Chocolate> chocolateContainer;
std::vector<Sugar> sugarContainer;
void chocolateVectorAdd(Chocolate element); // first function adding to one vector
void sugarVectorAdd(Sugar element); // second function adding to another vector
}
Please use example code, thanks :)
having lots of functions like vectorOneAdd, vectorTwoAdd used in order to add elements to the vector is pointless. There must be a better way
There is:
class Cookie {
std::vector<Chocolate> chocolateContainer;
std::vector<Sugar> sugarContainer;
private:
template<typename T>
std::vector<T>& get_vector(const T&); // not implemented but particularized
// write one of these for each vector:
template<>
std::vector<Chocolate>& get_vector(const Chocolate&) { return chocolateVector; }
template<>
std::vector<Sugar>& get_vector(const Sugar&) { return sugarVector; }
public:
template<typename T>
void add(T element) {
auto& v = get_vector(element);
v.push_back(std::move(element));
}
};
Following will solve your problem (need C++11)
#if 1 // std::get<T>(tuple) is not in C++11
// Helper to retrieve index to be able to call std::get<N>(tuple)
template <typename T, typename ... Ts> struct get_index;
template <typename T, typename ... Ts>
struct get_index<T, T, Ts...> : std::integral_constant<std::size_t, 0> {};
template <typename T, typename Tail, typename ... Ts>
struct get_index<T, Tail, Ts...> :
std::integral_constant<std::size_t, 1 + get_index<T, Ts...>::value> {};
#endif
template <typename ... Ts>
class Data
{
public:
template <typename T>
void push_back(const T& x) { return getVector<T>().push_back(x); }
template <typename T>
std::size_t size() const { return getVector<T>().size(); }
private:
template <typename T>
const std::vector<T>& getVector() const { return std::get<get_index<T, Ts...>::value>(items); }
template <typename T>
std::vector<T>& getVector() { return std::get<get_index<T, Ts...>::value>(items); }
private:
std::tuple<std::vector<Ts>...> items;
};
Test it:
class A{};
class B{};
int main()
{
Data<A, B> d;
d.push_back(A());
d.push_back(B());
return 0;
}