R value reference in function pointer

Viewed 20

I have seen a usage in function pointer:

void subMyCallback3(MyCallback&& callback) {
    callback(1);
}

I could not see any difference in an example project like the one below.

#include <iostream>
#include <functional>
#include "stdio.h"

using namespace std;

using MyCallback = std::function<void(int x)>;

void subMyCallback1(MyCallback callback) {
    printf("%p \n", &callback);
    callback(1);
}


void subMyCallback3(MyCallback&& callback) {
    printf("%p \n", &callback);
    callback(1);
}

int main() {
    auto func = [](int x) {
        std::cout << x << std::endl;    
    };
    printf("%p \n", &func);
    subMyCallback1(func);
    subMyCallback3(std::move(func));
    return 0;
}

Output:

0x7fff8ade477f 
0x7fff8ade4780 
1
0x7fff8ade4780 
1

What's the advantage of using it like this?

0 Answers
Related