mardi 17 septembre 2019

C++ Idioms: How can an rvalue be passed around as rvalue?

The following code doesn't compile. Clang gives this error message: candidate function not viable: no known conversion from 'A' to 'A &&' for 1st argument

this is as if a inside f() were an lvalue.

struct A{};

void g(A&& a){
    return;
}

void f(A&& a){
    g(a);
}

int main(){
    return 0;
}

This is reasonable because prevent f() from calling g() more than once.

The following would fix my code:

g(std::move(a)); 

but looks wrong at me: think what happens if someone modifies the signature of f() to void f(A& a).

Is there a better idiom?

Aucun commentaire:

Enregistrer un commentaire