简体   繁体   English

std :: function中带有静态函数的“未解析的重载函数类型”

[英]“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 . 尝试将重载的静态函数传递给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 . 但是,即使那里的答案用于将正确函数的地址转换为函数指针,它们也会失败并使用std::function Here is my MWE: 这是我的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? 有人可以解释为什么static_cast在隐式的时候不起作用?

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));
//                           ^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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