Tôi muốn xây dựng một hàng đợi mã băm bằng cách sử dụng mẫu variadic. Ví dụ mã tối thiểu làTạo hàng đợi băm với mẫu variadic
template<typename T>
void hash_queue(queue<size_t>& q){
q.push(typeid(T).hash_code());
}
template<typename T, typename... Ts>
void hash_queue(queue<size_t>& q){
hash_queue<Ts...>(q);
q.push(typeid(T).hash_code());
}
int main(){
queue<size_t> q;
hash_queue<int, float, double>(q);
return 0;
}
On biên dịch tôi nhận được
main.cpp: In instantiation of ‘void hash_queue(std::queue<long unsigned int>&) [with T = float; Ts = {double}]’:
main.cpp:19:22: required from ‘void hash_queue(std::queue<long unsigned int>&) [with T = int; Ts = {float, double}]’
main.cpp:25:35: required from here
main.cpp:19:22: error: call of overloaded ‘hash_queue(std::queue<long unsigned int>&)’ is ambiguous
hash_queue<Ts...>(q);
^
main.cpp:19:22: note: candidates are:
main.cpp:13:6: note: void hash_queue(std::queue<long unsigned int>&) [with T = double]
void hash_queue(queue<size_t>& q){
^
main.cpp:18:6: note: void hash_queue(std::queue<long unsigned int>&) [with T = double; Ts = {}]
void hash_queue(queue<size_t>& q){
Làm thế nào tôi có thể giải quyết này? Tôi không muốn tạo các phiên bản của các loại. Những loại sẽ là lớp đối tượng với các lớp constructor
Cảm ơn! Làm cách nào tôi có thể bao gồm lệnh gọi 'hash_queue (q);'? –
ztik
Rất tiếc, đã sửa trong bản chỉnh sửa của tôi. – TartanLlama