在 std::function 中使用静态函数的"unresolved overloaded function type"
"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));
// ^^^
相关文章:
- "Called Object Type is Not a Function or Pointer" 与 typedef 和类
- C++:"Expected '(' for function-style cast or type construction"错误
- 如何修复" 'Function' does not name a type "?[阿杜伊诺]
- 如何修复空虚拟方法的"warning: no return statement in function returning non-void [-Wreturn-type]"
- c++ (XORString): "recursive type or function dependency context too complex"
- "unresolved overloaded function type"错误并将运算符作为函数参数传递
- 在 2D 阵列分配期间对 calloc 进行"expression must have (pointer-to) function type"
- "overloaded function differs only by return type"错误
- 在 std::function 中使用静态函数的"unresolved overloaded function type"
- "Expected '(' for function-style cast or type construction"错误是什么意思?
- "Unresolved overloaded function type"尝试将for_each与迭代器一起使用并在C++中发挥作用
- c++声明顺序(const/type/function)
- 错误:调用'sort(..., ..., <unresolved overloaded function type>)'没有匹配函数
- 如何解决错误:"no matching function for call to ‘bind(<unresolved overloaded function type>"在类中使用 std
- 如何修复'unresolved overloaded function type?'
- 从 boost::thread 调用模板化函数时"unresolved overloaded function type"
- 模板函数指针:"overloaded function with no contextual type information"
- 调用'QDomDocument::createElement(<unresolved overloaded function type>)'没有匹配函数
- G++ 使用模板参数提供"unresolved overloaded function type"
- 看不到"control reaches end of non-void function [-Wreturn-type]"的原因