Using direct function VS std::invoke

Viewed 143

does this have an effect on performance? Which one makes more sense to use? What are the differences between them?

void foo(x, y)

foo (x, y); vs std::invoke(foo, x, y);

1 Answers

Godbolt suggests that there's no difference (at least in a simple case).

#include <functional>

extern int sum (int a, int b);

int main ()
{
    int x = sum(3, 4);
    int y = std::invoke(sum, 3, 4);
}

Assembly output:

main:
        sub     rsp, 8
        mov     esi, 4        # pushing parameter to call sum
        mov     edi, 3
        call    sum(int, int) # call to sum
        mov     esi, 4        # pushing parameters to call std::invoke sum
        mov     edi, 3
        call    sum(int, int) # calling std::invoke sum
        xor     eax, eax
        add     rsp, 8
        ret

As you can see, both assembly instructions are the same, so you can expect exactly the same runtime performance.

Related