简体   繁体   中英

“unresolved overloaded function type” with static function in std::function

I am getting an "unresolved overloaded function type" error when trying to pass an overloaded static function to an std::function .

I am aware of similar questions, such as this and this . However, even though the answers there work for getting the address of the right function into a function pointer, they fail with std::function . Here is my 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));
}

Could someone explain why the static_cast doesn't work when the implicit one does?

You can't cast to a function type . You probably meant to cast to a pointer type :

hello(static_cast<std::string(*)(const char*, size_t)>(&ClassA::DoCompress));
//                           ^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM