mardi 27 novembre 2018

returning a rvalue parameter as a rvalue reference

On this thread (Should I return an rvalue reference parameter by rvalue reference?), it is written that The parameter cannot be a temporary (which is just what rvalue references represent). If I understand this sentence well, this code should not be correct

#include <string>
#include <iostream>
std::string &&f(std::string &&a) {
    return std::move(a);   
}

int main() {
    std::string a = f("lol");
    std::cout << a << std::endl;
    return 0;
}

However, when we look to std::get, it seems that it returns rvalue reference for temporary tuple, and I actually think that there is no issue with using std::get with temporary tuple.

So, I think I misunderstand the sentence above, and the prior code is correct. However, this one is not correct :

#include <string>
#include <iostream>
std::string &&f(std::string &&a) {
    return std::move(a);   
}

int main() {
    std::string &&a = f("lol");
    std::cout << a << std::endl;
    return 0;
}

It would be correct if the function returns a value and not rvalue reference. Am I correct?

Aucun commentaire:

Enregistrer un commentaire