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

Extraneous move-from-local in return statements #5

Closed
Quincunx271 opened this issue Oct 14, 2018 · 1 comment
Closed

Extraneous move-from-local in return statements #5

Quincunx271 opened this issue Oct 14, 2018 · 1 comment
Labels
bug Something isn't working

Comments

@Quincunx271
Copy link
Contributor

Clang caught some unneeded std::moves which inhibit copy elision:

fs.hpp line 39

inline std::fstream open(const fs::path& filepath, std::ios::openmode mode) {
    std::error_code ec;
    auto            ret = open(filepath, mode, ec);
    if (ec) {
        throw std::system_error{ec, "Open file: " + filepath.string()};
    }
    return std::move(ret); // error: moving a local object in a return statement prevents copy elision [-Werror,-Wpessimizing-move]
}

fs.cpp line 13

std::fstream pf::open(const fs::path& filepath, std::ios::openmode mode, std::error_code& ec) {
    std::fstream ret;
    auto         mask = ret.exceptions() | std::ios::failbit;
    ret.exceptions(mask);

    try {
        ret.open(filepath.string(), mode);
    } catch (const std::ios::failure& e) {
        ec = e.code();
    }
    return std::move(ret); // error: moving a local object in a return statement prevents copy elision [-Werror,-Wpessimizing-move]
}

In both these cases, the return std::move(ret); should simply be return ret;

@vector-of-bool vector-of-bool added the bug Something isn't working label Oct 15, 2018
@Quincunx271
Copy link
Contributor Author

Fixed by 6c40d62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants