std::async 与重载函数 [英] std::async with overloaded functions

查看:136
本文介绍了std::async 与重载函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:

std::bind 重载解析

考虑以下 C++ 示例

Consider following C++ example

class A
{
public:
    int foo(int a, int b);
    int foo(int a, double b);
};

int main()
{
    A a;
    auto f = std::async(std::launch::async, &A::foo, &a, 2, 3.5);
}

这给出了 'std::async' :无法推断模板参数,因为函数参数不明确.我该如何解决这种歧义??

This gives 'std::async' : cannot deduce template argument as function argument is ambiguous. How do I resolve this ambiguity??

推荐答案

帮助编译器解决歧义,告诉您需要哪种重载:

Help the compiler resolve ambiguity telling which overload you want:

std::async(std::launch::async, static_cast<int(A::*)(int,double)>(&A::foo), &a, 2, 3.5);
//                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

或改用 lambda 表达式:

or use lambda expression instead:

std::async(std::launch::async, [&a] { return a.foo(2, 3.5); });

这篇关于std::async 与重载函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆