dimanche 10 février 2019

Taking in variables and returning a tuple for mutation

I'm trying to change the state of a couple of variables by passing it to a function, but without passing them as references. I find it cleaner to take in variables and return them.

I don't like using std::move so often, but it looks like I have to use them to avoid the copies?

Is the following way of doing so ok, or is it more of an anti-pattern?

std::tuple<A, B> foo(A a, B b) {
    // Some logic that needs A and B
    // Mutates A and B
    return std::make_tuple(std::move(a), std::move(b));
}

int main() {
    A a;
    B b;
    std::tie(a, b) = foo(std::move(a), std::move(b));
}

Aucun commentaire:

Enregistrer un commentaire