What is std::invoke in c++?

21,950

Solution 1

std::invoke takes something callable, and arguments to call it with, and does the call. std::invoke( f, args... ) is a slight generalization of typing f(args...) that also handles a few additional cases.

Something callable includes a function pointer or reference, a member function pointer, an object with an operator(), or a pointer to member data.

In the member cases, the first argument is interpreted as the this. Then remaining arguments are passed to () (except in the pointer-to-member-data-case), with std::reference_wrappers unwrapping.

INVOKE was a concept in the C++ standard; C++17 simply exposed a std::invoke which does it directly. I suspect it was exposed partly because it is useful when doing other metaprogramming, partly because every standard library has an implementation of INVOKE in it already and exposing it was basically free, and partly because it makes talking about INVOKE easier when it is a concrete thing.

Solution 2

A Callable object is, apart from C++-specific details, "something that can be called". It need not be a function: C++ has a number of types that can be called, and going through them every time where any could show up (read: generic code) is problematic and too repetitive.

That's what std::invoke is for - it allows a generic object that can be called (which, according to C++17, satisfies the Callable concept) to be invoked effortlessly.

Let's consider a simple example:

void foo() { std::cout << "hello world\n"; };

template <bool b>
struct optionally_callable
{
        std::enable_if_t<b> operator() ()  {   std::cout << "hi again\n";   }
};

int main()
{
    auto c = [] { std::cout << "hi from lambda\n" ;};

    std::invoke(foo);
    std::invoke(c);

    auto o = optionally_callable<true>{};
    //auto o2 = optionally_callable<false>{};

    std::invoke(o);

}

o2 is not callable, that is, std::is_invocable<decltype(o2)>::value is false.

Share:
21,950
user3397145
Author by

user3397145

Updated on July 10, 2022

Comments

  • user3397145
    user3397145 almost 2 years

    I've just been reading about std::thread and std::bind which I've faced with the Callable concept and std::invoke.

    I read about std::invoke on cppreference but I didn't understand what it said. Here is my question:
    What is std::invoke, std::function, std::bind and the Callable concept? And what is relationship between them?

  • Vittorio Romeo
    Vittorio Romeo about 7 years
    Note that std::invoke is not constexpr.
  • user3397145
    user3397145 about 7 years
    @Yakk thanks. I think i was confused INVOKE concept with std::invoke. and if I understand correctly, INVOKE concept is used in std::bind, std::thread. right?
  • edmz
    edmz about 7 years
    Points for improvement would be?
  • Baruch
    Baruch over 6 years
    Explain why this is better then writing foo(), c() and o()
  • alfC
    alfC almost 5 years
    @user3397145 I still don't understand why is this in all capitals letter like no other thing in the C++ documentation.
  • alfC
    alfC almost 5 years
  • Emile Cormier
    Emile Cormier over 3 years
    @VittorioRomeo Hi, I am from the future. std::invoke is constexpr as of c++20.