Skip to content

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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

Sebanisu
Copy link
Contributor

@Sebanisu Sebanisu commented Mar 22, 2021

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 the find function. I think it's better to forward anything any_of doesn't use.
I rewatched this talk for a refresher.
CppCon 2019: Klaus Iglberger “Back to Basics: Move Semantics (part 2 of 2)”

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
@Sebanisu
Copy link
Contributor Author

Removing as const. As this isn't something the standard does. We aren't mutating the data but the caller might want to.

@Sebanisu Sebanisu changed the title Use for loop, (void) and std::as_const Use for loop, (void) Mar 23, 2021
I found that mutable lambda's can't be passed to the algorithms with const.
fixes: codereport#13
I only didn't forward the end iterator. As we use it again for return.

https://www.godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAM1QDsCBlZAQwBtMQBGAFlJvoCqAZ0wAFAB4gA5AAYppAFZdSrZrVDIApACYAQjt2kR7ZATx1KmWugDCqVgFcAtrRDbSV9ABk8tTADlnACNMYi4AdlIAB1QhQnNaO0cXZRi4szofP0CnELDOd2NMUwSGAmZiAiTnV0LMEwzaMoqCLIDg0IijcsrqlM5ulracvK5tAEojVAdiZA4pHQBmX2RHLABqTUWbCuJmAE8t7E0ZAEEllbXMTe3zIQJiTGYnI5Pz7WXaVYcNrZsCBxRdivM4ETBOIHMME3f77KJWZ7XACSpHWBDhCKcmAAdLj1kihKj0fDaIj1qIQecAKz6Gm0DB4IQsYjoTRUgAibPZ6zQtHumHEUWI62YDiI6yovnQEFE6yFmHQqKREqVPLo93WrCVQlx2IlQnGm3C%2BjO6wlJHWEC2ugl6zAYC23NY1s2egMVFRUAAbqg8OhxgZ3YT1rrxobNMa3mazXgqJb5dKAFQe9bJnW4sPrIKPZgAa2tUaNnNN60eAOItHW93QIBAAKBmAjuhTVHT2IjnMWJvO4WL51B4Mh0L%2BxMxyKJGNJWN1%2BODo6n1wpi2OZzZtN09PwTIqrI5XLVfLBguFovFan2AH1UFQZToAGz3uWPRX4%2B%2BPlPK3karWzt/aO8zq24aRiWp6oAeGoEDc3KStYEDVrWNDEAA7jufxLtgEAJpMVYEDWIBIahLJ/EiRzUDhWqFtG1E0bRZoIQRJBEay2wEmRQGhgWJZljMlYMcAmAEH8MhkQQhr2o6mpcT2fZvGCEKqMO2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FaNDpMBATwsBhHpgY%2BQK9wgyGxJwcIiwqRDLtgMqkvTVqOL9lIFxwdtGTF0QBbQbtrGcPCOEbQnD7Ge0WKwpx/s3HzIyVIbQezplNKNpMPuRcEhmyAA
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

Successfully merging this pull request may close these issues.

const may not be correct afterall.
1 participant