所以我有一个需要传递给线程的重载模板化函数。我不知道如何区分重载函数和函数指针。
template<typename T>
void DetectChange(T& variable, T& notify) { // must be a thread, else it's useless
T original = variable;
while (true) {
if (variable != original) { // change detected
notify = variable; // send notification
variable = original; // reset to original
}
}
}
template<typename T>
void DetectChange(T& variable, void (* notify)()) { // must be a thread, else it's useless (template, function pointer)
T original = variable;
while (true) {
if (variable != original) { // change detected
notify(); // do notification function
variable = original; // reset to original
}
}
}
int main() {
int x = 3;
void(*function)();
function = &DetectChange; // how to distinguish which overloaded templated function
std::thread detect = std::thread(&function, x, doSomething);
x++; // change variable
return 0;
}
答案 0 :(得分:1)
您的问题是该功能与过载都不匹配。声明函数不带参数,但两个可用的重载至少需要两个。并且函数指针类型必须匹配到引用等。
void (*ptr)(int &, int&) = &DetectChange;
当然,这将无法编译,因为int不是有效的T,但它应该给你一个想法。