Today, while using the same template for the hundredth time, I came across the idea to introduce a simple replacement.
Instead of
QHash<int,QHash<int,QHash<int,int> > >
it should now be:
MultiKeyHash<int,int,int,int>
(Not to be confused with QT's QMultiHash class).
Whereas the first three arguments are the types of the keys and the last argument is the type of the value.
MultiKeyHash<TKey1,TKey2,....,TKeyN,TValue>
My current implementation looks like this:
template <typename... Args>
struct MultiKeyHash;
template <typename TKey1, typename... Args>
struct MultiKeyHash<TKey1, Args...> : QHash<TKey1,MultiKeyHash<Args...> > {
typedef typename MultiKeyHash<Args...>::ValueType ValueType;
const ValueType& value(const TKey1 &key1, const Args_Without_Last&... args) const {
return operator[](key1).value(args...);
}
};
template <typename TKeyN, typename TValue>
struct MultiKeyHash<TKeyN,TValue> : QHash<TKeyN,TValue> {
typedef TValue ValueType;
};
Everything worked as expected until I wanted to add the "value" method. I certainly can't use "Args" as Type for the second parameter(-pack). Do I have to make a function template of it, or is there a way around?
How do I have to define the "value" method in order to be able to call it like:
value(key1,key2,...,keyn)