[英]no matching function for call to <unresolved overloaded function type>
I can't relate with similar questions.我无法回答类似的问题。 That's my MRE, basically I'd like to overload
fun
with a version accepting a template reference.那是我的 MRE,基本上我想用一个接受模板引用的版本来重载
fun
。 It all works until std::thread
enters in the game.这一切都有效,直到
std::thread
进入游戏。 It seems I'm missing something from its constructor.看来我从它的构造函数中遗漏了一些东西。
Error shown on g++-10 is g++-10 上显示的错误是
error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, std::string, std::shared_ptr<MySem>)’
43 | std::make_shared<MySem>());
#include <string>
#include <memory>
#include <thread>
class MyData
{};
class MySem
{};
template <typename T, typename Sem>
void fun(T & t, const std::string & TAG, std::shared_ptr<Sem> upSem)
{}
template <typename T, typename Sem>
void fun(const std::string & TAG, std::shared_ptr<Sem> upSem)
{
T t;
fun(t, TAG, upSem); // NO std::ref(t)
}
int main(int argc, char ** argv)
{
MyData d;
fun<MyData, MySem>(
"works",
std::make_shared<MySem>());
fun<MyData, MySem>(
d,
"this too",
std::make_shared<MySem>());
std::thread t1(fun<MyData, MySem>,
std::string("this doesn't"),
std::make_shared<MySem>()); // line 43
std::thread t2(fun<MyData, MySem>,
d,
std::string("this neither"),
std::make_shared<MySem>());
return 0;
}
If you have an overloaded function and want to pick a specific overload, you have to cast it manually to get the right one like:如果您有一个重载的 function 并且想要选择一个特定的重载,您必须手动转换它以获得正确的重载,例如:
std::thread t1(static_cast<void(*)(const std::string&, std::shared_ptr<MySem>)>(&fun<MyData, MySem>),
std::string("this doesn't"),
std::make_shared<MySem>()); // line 43
As already given in the comment from "Passer By", using a lambda simplifies the whole thing a lot and makes the code more readable.正如“路人”的评论中已经给出的那样,使用 lambda 可以大大简化整个过程并使代码更具可读性。
See also here: other answer另请参阅此处: 其他答案
My guess is that the constructor of std::thread
cannot resolve which overload of fun
you're trying to call.我的猜测是
std::thread
的构造函数无法解析您要调用的fun
重载。 No idea why though.不知道为什么。
Having only one version of fun
such as只有一种
fun
,例如
template <typename T, typename sem>
void fun(const std::string&, std::shared_ptr<sem>)
{
...
}
Allows you to construct t1
fine (but t2
will obviously fail).允许您构建
t1
好(但t2
显然会失败)。
A workaround is to pass a lambda instead, such as:解决方法是改为传递 lambda,例如:
std::thread t3([&](){fun<data, sem>(d, "works again", std::make_shared<sem>());});
std::thread t4([&](){fun<data, sem>("this too", std::make_shared<sem>());});
The problem is not related with the std::thread
function, the problem is that the compiler does not know which overloaded function you are requesting, using a lambda as a workaround could do the job:问题与
std::thread
function 无关,问题是编译器不知道您请求的是哪个重载 function,使用 lambda 作为变通方法可以完成这项工作:
std::thread t1([=]{
fun<MyData, MySem>(std::string("This doesn't"), std::make_shared<MySem>());
} );
t1.join();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.