An-Algorithm-Library icon indicating copy to clipboard operation
An-Algorithm-Library copied to clipboard

Use for loop, (void)

Open Sebanisu opened this issue 4 years ago • 1 comments

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.

  • 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://stackoverflow.com/questions/38357089/why-does-stdtransform-and-similar-cast-the-for-loop-increment-to-void
  • https://en.cppreference.com/w/cpp/language/operator_other
  • https://www.reddit.com/r/cpp/comments/1tedni/notsocommon_pitfalls_of_c_xpost_rcplusplus/ce7bqnu/ C++Now 2017: Jason Turner "(Ab)using C++17"

Remove const

Mutable lambdas couldn't be passed to these functions. So we had to remove const fixes: https://github.com/codereport/An-Algorithm-Library/issues/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)”

Sebanisu avatar Mar 22 '21 14:03 Sebanisu

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

Sebanisu avatar Mar 23 '21 08:03 Sebanisu