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

emplace_back instead of push_back #57

Closed
taion opened this issue May 4, 2015 · 5 comments
Closed

emplace_back instead of push_back #57

taion opened this issue May 4, 2015 · 5 comments

Comments

@taion
Copy link

taion commented May 4, 2015

This is horribly trivial, but is there a particular reason not to use emplace_back instead of push_back here?

I just happened to notice it while looking at #55.

@ecorm
Copy link
Owner

ecorm commented May 5, 2015

Since push_back is taking an r-value reference to the container's value type, there's nothing to be gained by using emplace_back. Note that vector provides a push_back overload that takes an r-value reference.

emplace_back is useful when you want to "directly" construct the new element with one or more arguments. For example, if that line had been:

array.push_back(3)

that would construct a temporary Variant from the argument 3, and then initialize the new element in the vector. In that scenario, it would have been better to use:

array.emplace_back(3)

This SO answer confirms my reasoning.

P.S.: I might take a couple days before I get the chance to go through your PR. Thanks for the contribution!

@taion
Copy link
Author

taion commented May 5, 2015

With emplace_back, I think you'd write that line as:

array.emplace_back();

which would create the Variant in-place instead of moving in the temporary, per the SO answer.

@ecorm
Copy link
Owner

ecorm commented May 5, 2015

Ah, yes. That makes sense. Even if push_back or emplace_back were equivalent in this case, I think I'll change it to emplace_back anyway, to avoid this question being asked again.

If you end up making another commit in your PR, you may go ahead and change it. This change is too trivial to warrant a separate PR.

taion pushed a commit to taion/cppwamp that referenced this issue May 5, 2015
@taion
Copy link
Author

taion commented May 5, 2015

On second thought, shouldn't this just be

    array.emplace_back(std::get<N>(std::move(tuple)));
    assignFromTuple<N+1, Ts...>(array, std::move(tuple));

I see the tests cover the toArray function, and they still pass with this change. Am I missing something?

@ecorm
Copy link
Owner

ecorm commented May 5, 2015

Yes, you're absolutely right. I had not looked at the "bigger picture" when you first raised the issue. Thanks for catching this. Can you slip that in your PR?

taion pushed a commit to taion/cppwamp that referenced this issue May 5, 2015
taion pushed a commit to taion/cppwamp that referenced this issue May 5, 2015
taion pushed a commit to taion/cppwamp that referenced this issue May 5, 2015
@ecorm ecorm closed this as completed in 210cf2b May 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants