Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

framework: fix rvalue reference warnings #5948

Merged

Conversation

PragmaTwice
Copy link
Contributor

@PragmaTwice PragmaTwice commented Aug 18, 2021

To fix this error reported by Clang:

home/twice/project/oneflow/oneflow/core/framework/tensor_consistent_id.h:43:12: error: local variable 'ret' will be copied despite being returned by name [-Werror,-Wreturn-std-move]
    return ret;
           ^~~

In this case, create an rvalue reference and return it (as lvalue) can cause a copy initialization.
I made an example to illuminate this behavior:

struct X {
    X() { std::cout << "X\n"; }
    X(const X&) { std::cout << "const X&\n"; }
    X(X&&) { std::cout << "X&&\n"; } 
};

X g() {
    return X();
}

X f() {
    X x = g();      // case 1
    // X&& x = g(); // case 2
    return x;
}

int main() {
    X a = f();
}

In case 2, we will find an unnecessary X(const X&) will be called.
You can open https://godbolt.org/z/a91c1snza to debug this example online.

@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 18, 2021 12:31
@oneflow-ci-bot oneflow-ci-bot self-requested a review August 18, 2021 14:02
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 18, 2021 15:24
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 18, 2021 17:05
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 18, 2021 19:22
@oneflow-ci-bot oneflow-ci-bot merged commit 43be27a into Oneflow-Inc:master Aug 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants