簡體   English   中英

std :: function中帶有靜態函數的“未解析的重載函數類型”

[英]“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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM