-
Notifications
You must be signed in to change notification settings - Fork 4
Use for loop, (void) #11
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
Open
Sebanisu
wants to merge
5
commits into
codereport:main
Choose a base branch
from
Sebanisu:void-and-for
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://godbolt.org/z/W1oM15Evj I just made some test code things appear to still work. For loop can iterate and check the condition in the same line. https://blog.codeisc.com/2018/01/09/cpp-comma-operator-nice-usages.html https://blog.codeisc.com/2017/12/26/cpp-comma-operator-introduction.html https://stackoverflow.com/questions/39514765/the-void-the-comma-operator-operator-and-the-impossible-overloading https://youtu.be/AqDsso3S5fg?t=276 (void), comma operator can be overloaded. So you need to cast to void to force using the built-in comma operator. https://stackoverflow.com/questions/5853833/casting-non-const-to-const-in-c https://en.cppreference.com/w/cpp/utility/as_const I was worried that a pred could mutate the values as there was nothing to prevent this. As the Iterator needs to be mutable so you can iterate. The thing it points too may or may not be const. So someone could write a pred that mutates. Same with Op for transform. You don't want to mutate the incoming just the out going value.
Since the comma operator takes 2 parameters you don't need to cast all of them to void but at least 1. I'm being safe and since each one is called left to right and each call is it's own group of 2 I'm casting (void) on all but the first one. https://stackoverflow.com/questions/38357089/why-does-stdtransform-and-similar-cast-the-for-loop-increment-to-void
Removing as const. As this isn't something the standard does. We aren't mutating the data but the caller might want to. |
I found that mutable lambda's can't be passed to the algorithms with const. fixes: codereport#13
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
https://godbolt.org/z/rrnzhdoGn
I just made some test code things appear to still work.
Using for loop instead of while
For loop can iterate and check the condition in the same line.
Cast to void
(void), comma operator can be overloaded. So you need to cast to void to force using the built-in comma operator. I'm not sure you can defend against all uses of an overloaded comma operator. I'm not 100% sure when this is needed. So I used it in places where the return value isn't required. Like when you are iterating the iterators. Someone said it's a very uncommon overload and they only saw it used in boost. Though it can happen. std::transform uses the (void) cast.
Remove const
Mutable lambdas couldn't be passed to these functions. So we had to remove const
fixes: #13
Forwarding Ref
any_of
is passing the members directly to thefind
function. I think it's better to forward anythingany_of
doesn't use.I rewatched this talk for a refresher.