I'm trying to make a template of multi key map (just to get used to variadic templates). Here's the code:
template<class Value, class Key1, class ...Keys>
class QMultiKeyMap;
// single key map
template<class Value, class Key1>
class QMultiKeyMap<Value, Key1>
{
QMap<Key1, Value*> data;
protected:
// ignore inserting without keys
void insert(Value*) {} // This is the function I want to call
public:
QMultiKeyMap() {}
void insert(const Value& v, const Key1& k1)
{
Value* pV = new Value(v);
data.insert(k1, pV);
}
};
// map with at least two keys
template<class Value, class Key1, class Key2, class ...Keys>
class QMultiKeyMap<Value, Key1, Key2, Keys...>: public QMultiKeyMap<Value, Key2, Keys...>
{
QMap<Key1, Value*> data;
protected:
typedef QMultiKeyMap<Value, Key2, Keys...> base;
// inserting with mix of keys, but with Key1 too
// (cases without Key1 are inherited)
template<class ...KeyOthers>
void insert(Value* pV, const Key1& k1, const KeyOthers& ...keys)
{
// in case keys... is not empty, call itself recursively.
// If keys... is empty, then there's the empty function insert(Value*) in grandparent class (QMultiKeyMap with one key) which should be called.
insert(pV, keys...);
data.insert(k1, pV);
}
public:
QMultiKeyMap() {}
// inserting value with mix of keys
template<class KeyOne, class ...KeyOthers>
void insert(const Value& v, const KeyOne& k1, const KeyOthers& ...keys)
{
Value* pV = new Value(v);
insert(pV, k1, keys...);
}
};
I'm trying to call:
QMultiKeyMap<int, quint32, quint16, quint8> test;
// single key insertion - the value is 1, the key is quint32(1)
test.insert(1, quint32(1));
But I'm getting 'no matching function' error inside QMultiKeyMap::insert(Value* pV, const Key1& key1, const KeyOthers& ...keys). The compiler doesn't see the grandparent function, it doesn't search that class. How can I tell the compiler to do it?
I suspect it has something to do with shadowing like in this question. But I don't know how should I write 'using' directive which refers to the most base class of the hierarchy. Should I use something like that?
The compilation trace is:
In file included from ..\signalTest\main.cpp:1:0:
..\signalTest\handler.h: In instantiation of 'void QMultiKeyMap<Value, Key1, Key2, Keys ...>::insert(Value*, const Key1&, const KeyOthers& ...) [with KeyOthers = {}; Value = int; Key1 = unsigned int; Key2 = short unsigned int; Keys = {unsigned char}]':
..\signalTest\handler.h:91:38: required from 'void QMultiKeyMap<Value, Key1, Key2, Keys ...>::insert(const Value&, const KeyOne&, const KeyOthers& ...) [with KeyOne = unsigned int; KeyOthers = {}; Value = int; Key1 = unsigned int; Key2 = short unsigned int; Keys = {unsigned char}]'
..\signalTest\main.cpp:8:89: required from here
..\signalTest\handler.h:67:27: error: no matching function for call to 'QMultiKeyMap<int, unsigned int, short unsigned int, unsigned char>::insert(int*&)'
insert(pV, keys...);
^
..\signalTest\handler.h:67:27: note: candidates are:
..\signalTest\handler.h:64:43: note: template<class ... KeyOthers> void QMultiKeyMap<Value, Key1, Key2, Keys ...>::insert(Value*, const Key1&, const KeyOthers& ...) [with KeyOthers = {KeyOthers ...}; Value = int; Key1 = unsigned int; Key2 = short unsigned int; Keys = {unsigned char}]
void insert(Value* pV, const Key1& k1, const KeyOthers& ...keys)
^
..\signalTest\handler.h:64:43: note: template argument deduction/substitution failed:
..\signalTest\handler.h:67:27: note: candidate expects 3 arguments, 1 provided
insert(pV, keys...);
^
..\signalTest\handler.h:88:56: note: template<class KeyOne, class ... KeyOthers> void QMultiKeyMap<Value, Key1, Key2, Keys ...>::insert(const Value&, const KeyOne&, const KeyOthers& ...) [with KeyOne = KeyOne; KeyOthers = {KeyOthers ...}; Value = int; Key1 = unsigned int; Key2 = short unsigned int; Keys = {unsigned char}]
void insert(const Value& v, const KeyOne& k1, const KeyOthers& ...keys)
^
..\signalTest\handler.h:88:56: note: template argument deduction/substitution failed:
..\signalTest\handler.h:67:27: note: cannot convert 'pV' (type 'int*') to type 'const int&'
insert(pV, keys...);