-
Notifications
You must be signed in to change notification settings - Fork 5k
The upd_sink and dist_sink files have been modified to address Passed by value warnings #3520
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
The upd_sink and dist_sink files have been modified to address Passed by value warnings #3520
Conversation
… by value warnings.
| public: | ||
| // host can be hostname or ip address | ||
| explicit udp_sink(udp_sink_config sink_config) | ||
| explicit udp_sink(const udp_sink_config& sink_config) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change affects the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR addresses CppCheck "Passed By Value" performance warnings in the udp_sink and dist_sink header files by optimizing parameter passing strategies.
Changes:
- Modified
udp_sinkconstructor to acceptudp_sink_configby const reference instead of by value - Modified
dist_sinkconstructor to use move semantics for thesinksvector parameter
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| include/spdlog/sinks/udp_sink.h | Changed constructor parameter from pass-by-value to pass-by-const-reference |
| include/spdlog/sinks/dist_sink.h | Added std::move to constructor initialization to enable move semantics |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| explicit dist_sink(std::vector<std::shared_ptr<sink>> sinks) | ||
| : sinks_(sinks) {} | ||
| : sinks_(std::move(sinks)) {} |
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constructor parameter should be passed by value to enable the copy-and-swap idiom with std::move. However, for optimal flexibility, consider providing both const reference and rvalue reference overloads, or pass by const reference if move semantics are not intended. The current approach (pass-by-value + move) is valid but may not be the clearest API design without documentation explaining the ownership transfer semantics.
So whats the problem? this is the intended usage of move.. |
I may not have expressed myself clearly here. I adopted this approach so that there would be no change in behavior. The following examples and explanations will help you:
With Value signing correctly reflects the caller's intent:
My PR preserves this behavior. However, if your goal is to perform an operation without the user accessing the source code, you should use |
|
Thanks @KaganCanSit |
You're welcome. If there are any errors or if you need help, you can ping me. I'd be happy to help when I'm available. It was nice to spend time on a different project. |
Hello,
This pull request addresses CppCheck's
Passed By Valuewarnings. Additional information on this topic was provided in the discussion section here, and unnecessary/misleading warnings were removed from the list.Remaining Warnings;
I'm not sure about the explicit constructor in
dist_sink.h!When
vector<shared_ptr<...>>is copied here, each shared_ptr is copied, creating shared ownership of the relevant objects. In this case, ownership of the objects is shared between the caller and the objects.If the caller provides an rvalue with
std::move, the container and its elements are moved; the caller's argument becomes moved-from (valid but with undefined content). This does not guarantee absolute sole ownership of the managed objects; if there are other copies ofshared_ptr, ownership is already shared.Since this ctor stores the parameter internally, I passed it to the member using
by-value + std::moveto reduce unnecessary copying without changing the API. This approach does not move the caller's argument in lvalue calls; in rvalue calls, it allows direct transfer.However, I'm a bit hesitant about this improvement. Since this forward-looking constructor is not currently used in the project, it could be removed, or if clearer behavior is desired,
const&+&&overload alternatives could be considered.That said, I fixed warnings as much as possible outside of
fmtandtests. If you notice anything I missed, please let me know via comment/ping; I'll address it when I can. I'll keep the Discussion topic open for a while in case it turns out to be a bug (as I mentioned earlier). (I'm thinking a maximum of 1 week.)Thank you for your guidance.
Best regards.