top | item 45596162

(no title)

Strilanc | 4 months ago

> the people writing the standard are not exactly known for adding features “just because”

Ah yes, C++, the discerning language.

Iterating over optional does seem syntactically convenient. My main question would be if it guarantees no overhead. For example, is there an additional conditional branch due to the iterator hiding the statically know fact that there's at most one iteration? I don't use C++ for its beauty, I use it for speed.

discuss

order

pbsd|4 months ago

No, the generated code seems to be mostly the same as the manual version: https://gcc.godbolt.org/z/aK8orbKE8

The main difference there seems to be that GCC treats the if() as unlikely to be taken while the for() as likely.

loeg|4 months ago

> Ah yes, C++, the discerning language.

C++, the language that refused to add `contains()` to maps until, you know, C++20!

nikanj|4 months ago

They didn't stop there, C++23 brought contains() to strings too! You can do some crazy stuff nowadays like say if(username.contains("hello"))

Absolutely incredible, definitely worth the wait

nzeid|4 months ago

This gave me a good chuckle. For those wondering, `count` was the go-to and I can tolerate an argument that `contains` is a negligible improvement.