In my journey to implement a template-based linked-list which works with key/value-pairs I want to implement a "popHead()
"-method. However, I can't get it to compile.
/**
* Removes the first element in the list and returns it.
* @return first element, nullptr if list is empty
*/
std::tuple<K, V> popHead() {
auto tmp = head;
if (tmp) {
if (tmp->next) {
head = tmp->next;
} else {
head = nullptr;
}
return new std::tuple(tmp->key, tmp->value);
}
return nullptr;
};
This doesn't work because a type-specified is expected. Ok so it doesn't know what kind of type the values contained by the tuple should have.. however.. this doesn't work either:
return new std::tuple<K, V>(tmp->key, tmp->value);
How do I return a tuple with types <K, V>
?
Maybe
return {tmp->key, tmp->value};
?
Or, maybe, you want return a pointer to a std::tuple<K, V>
?
In this case
return new std::tuple<K, V>(tmp->key, tmp->value);
should work but you have to modify the return type
std::tuple<K, V> * popHead()
// --------------^ *pointer* to tuple