![](/img/trans.png)
[英]std::thread <unresolved overloaded function type> error
[英]“unresolved overloaded function type” with static function in std::function
嘗試將重載的靜態函數傳遞給std::function
時,我收到“未解析的重載函數類型”錯誤。
我知道類似的問題,比如這個和這個 。 但是,即使那里的答案用於將正確函數的地址轉換為函數指針,它們也會失敗並使用std::function
。 這是我的MWE:
#include <string>
#include <iostream>
#include <functional>
struct ClassA {
static std::string DoCompress(const std::string& s) { return s; }
static std::string DoCompress(const char* c, size_t s) { return std::string(c, s); }
};
void hello(std::function<std::string(const char*, size_t)> f) {
std::string h = "hello";
std::cout << f(h.data(), h.size()) << std::endl;
}
int main(int argc, char* argv[]) {
std::string (*fff) (const char*, size_t) = &ClassA::DoCompress;
hello(fff);
hello(static_cast<std::string(const char*, size_t)>(&ClassA::DoCompress));
}
有人可以解釋為什么static_cast
在隱式的時候不起作用?
您無法轉換為函數類型 。 你可能想要轉換為指針類型 :
hello(static_cast<std::string(*)(const char*, size_t)>(&ClassA::DoCompress));
// ^^^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.