简体   繁体   中英

Using std::invoke when a function is overloaded

I am trying to use std::invoke() with an overloaded function:

#include <iostream>
#include <functional>

struct S {
    void foo(int) { }
    void foo(int, int) { }
};

int main()
{
    S s;
    std::invoke(&S::foo, s, 1);
}

but I get an error: 'std::invoke': no matching overloaded function found . It works fine, when there is the only one function with name foo() . Is it possible to use std::invoke() when a function is overloaded?

There are two example solutions:

std::invoke(static_cast<void(S::*)(int)>(&S::foo), s, 1);
std::invoke([&s]() { s.foo(1); });

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