frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

C++26: range support for std:optional

https://www.sandordargo.com/blog/2025/10/08/cpp26-range-support-for-std-optional
47•birdculture•5d ago

Comments

criemen•2h ago
Great to see they learned from Java, which initially made the mistake of not supporting the streams interface for their Optional type at first [1]. It was infuriating.

[1] https://stackoverflow.com/questions/22725537/using-java-8s-o...

jb1991•1h ago
This is absolutely magnificent. Game-changing improvement, really shows how amazing the C++ committees are working.
Simran-B•1h ago
I'm sensing a pinch of sarcasm
steveklabnik•1h ago
When we added the equivalent to Rust, it was a bit controversial. But the idea of "an optional type is a list with zero or one items in it" is much more normal in functional places, and so a lot of people like it quite a bit too.
tialaramex•1h ago
The fun thing is that the original design (N3527 over a decade ago) for std::optional specifically says it's not the container with exactly zero or one items, whereas I believe Rust's Option never made this claim and so recognising this isn't a U-turn.
Strilanc•1h 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.

loeg•1h ago
> Ah yes, C++, the discerning language.

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

nikanj•1h 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

tialaramex•55m ago
Note that despite this work in C++ you can't flip the script to if ("hello".contains(username)) unlike Rust

Rust will also let you: "FOO".contains(char::is_uppercase)

That's showing off three clever tricks Rust has that C++ does not, but one day it will just be compile time evaluated as true and that's one place C++ is richer today. Many other things in Rust can be but Rust's traits aren't today allowed to be compile time evaluated, so the fact that we could determine at compile time whether there's an uppercase letter in FOO isn't enough yet. One day.

nzeid•1h 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.
DSingularity•1h ago
The for syntax over optional seems unnatural. I get that optional is like a list of 0 or 1 owns. Is there a fault in my reading that makes the syntax clunky?
steveklabnik•1h ago
With syntax questions, it can help to get very concrete. There's a few different bits of syntax in this post, what is it you find clunky?
kstrauser•23m ago
Not the person you were replying to, but

  for (auto l : logger) {
      l.log(data);
  }
bent my brain for a moment. `logger` is a list of loggers to send data do? Oh, no, it's either 0 or 1 loggers.

Rust's `if let` syntax maps much more closely to how I think about it. I guess if this becomes idiomatic C++ then it'd start looking perfectly normal to everyone, but it still seems odd. For instance, I don't think I could ever bring myself to write Python like this, even if it worked:

  def do_something(data, logger=None):
      for l in logger:
          l.log(data)
steveklabnik•12m ago
That makes sense! What's funny is, I went to go write the equivalent Rust code:

    struct Logger;
    
    impl Logger {
        fn log(&self, _: &str) {}
    }
    
    fn do_something(data: &str, logger: Option<Logger>) {
        for l in logger {
            l.log(data)
        }
    }
    
    fn main() {
        let logger = Some(Logger);
        let data = "foo";
        
        do_something(data, logger);
    }
and rustc actually warns about this:

    warning: for loop over an `Option`. This is more readably written as an `if let` statement
     --> src/main.rs:8:14
      |
    8 |     for l in logger {
      |              ^^^^^^
      |
      = note: `#[warn(for_loops_over_fallibles)]` on by default
    help: to check pattern in a loop use `while let`
      |
    8 -     for l in logger {
    8 +     while let Some(l) = logger {
      |
    help: consider using `if let` to clear intent
      |
    8 -     for l in logger {
    8 +     if let Some(l) = logger {
      |
I was actually going to say "I've never seen folks in Rust write out for loops this way, even though you can" and I suspect this lint has something to do with that!

I think this is most useful in generic contexts, I agree that I would not write this exact code this way because it does feel weird, if you know you have an Option, if let is the way to go. But if you're writing code over generic iterators, it can be nice that it exists.

mkehrt•1h ago
What seems weird here? Iterating or mapping over Options is pretty normal in functional languages.
lo_zamoyski•34m ago
Well, you can map over options/maybes. For-loop style iteration over an option does seem a little strange IMO, first, because syntactically it looks like overkill, and second (and more importantly), because map evals to a value, while `for` does not.

But I suppose in C++, given the centrality of looping constructs, this is what you would do to accommodate option.

lanyard-textile•6m ago
Putting it through a for loop demonstrates that it is iterable, imo.

  template <typename T>
  void foo(const T& container) {
    for (auto value : container) {
      // process value
    }
  }

  foo(std::vector<int>())
  foo(std::optional<int>())
loeg•1h ago
Seems like a good thing broadly. I haven't personally found a use for ranges, ever, but this seems consistent.
nmeofthestate•1h ago
Herb Sutter calls this kind of thing "spelling things generically" meaning that the same generic code will compile against different types. In this case the same for loop code will compile against a type that may hold zero or one items and a type that may contain 0->n items. Maybe this pattern could be extended to shared_ptr for example.
jmrm•53m ago
I really like how some good structures used by other languages, specially Rust and Zig, have been added to the newer C++ standard.

The Result, Optional, and Variant are really sweet for day-to-day use of the language, and those in-process standard libraries of SIMD operations, BLAS mathematical functions, and the the execution library looks really cool, specially as standard.

I would like C++ would be a little more "batteries included" in some ways, like having a basic standard for signals, networking (just handling sockets would be a huge thing), and some basic system calls.

jcelerier•27m ago
> I really like how some good structures used by other languages, specially Rust and Zig, have been added to the newer C++ standard. The Result, Optional, and Variant are really sweet for day-to-day use of the language, and those in-process standard libraries of SIMD operations, BLAS mathematical functions, and the the execution library looks really cool, specially as standard.

for Optional and Variant they both were basically standardized versions of boost.optional & boost.variant, which exist since 2003 and 2002 respectively. Most of the time you can just change boost:: to std:: and it works exactly the same ; for many years software I develop could switch from one to another with a simple #ifdef due to platforms not supporting std::optional entirely (older macOS versions, pre 10.14 IIRC)

forrestthewoods•24m ago
std::variant is an abomination that should never be used by anyone ever. Everything about it is sooooo bad.
nrclark•3m ago
out of curiosity, can you elaborate on that a bit? I've shipped std::variant in a few designs and it's been OK, but maybe I didn't use it deeply enough to see issues.
jasonthorsness•52m ago
It's sort of like a cleaner approach to a C# SelectMany where you return either an empty array or a single-item array?

C++ keeps getting more stuff but it's clear a lot of thought goes into these additions. I think a "living" language has proven better over time (C# evolved heavily and borrowed from other languages, C++ does as well, maybe lately concepts from functional and Rust?) Go might be the big exception that evolves super-slowly.

nurumaik•9m ago
Maybe somewhere in C++60 we will finally have proper monads

Apple M5 chip

https://www.apple.com/newsroom/2025/10/apple-unleashes-m5-the-next-big-leap-in-ai-performance-for...
735•mihau•6h ago•777 comments

Things I've learned in my 7 Years Implementing AI

https://www.jampa.dev/p/llms-and-the-lessons-we-still-havent
49•jampa•1h ago•16 comments

I almost got hacked by a 'job interview'

https://blog.daviddodda.com/how-i-almost-got-hacked-by-a-job-interview
449•DavidDodda•6h ago•222 comments

Pwning the Nix ecosystem

https://ptrpa.ws/nixpkgs-actions-abuse
188•SuperShibe•6h ago•27 comments

Claude Haiku 4.5

https://www.anthropic.com/news/claude-haiku-4-5
233•adocomplete•2h ago•87 comments

Claude Haiku 4.5 System Card [pdf]

https://assets.anthropic.com/m/99128ddd009bdcb/original/Claude-Haiku-4-5-System-Card.pdf
41•vinhnx•1h ago•3 comments

Clone-Wars: 100 open-source clones of popular sites

https://github.com/GorvGoyl/Clone-Wars
24•ulrischa•1h ago•0 comments

Show HN: Halloy – Modern IRC client

https://github.com/squidowl/halloy
203•culinary-robot•7h ago•64 comments

US Passport Power Falls to Historic Low

https://www.henleyglobal.com/newsroom/press-releases/henley-global-mobility-report-oct-2025
61•saubeidl•2h ago•68 comments

F5 says hackers stole undisclosed BIG-IP flaws, source code

https://www.bleepingcomputer.com/news/security/f5-says-hackers-stole-undisclosed-big-ip-flaws-sou...
71•WalterSobchak•6h ago•31 comments

C++26: range support for std:optional

https://www.sandordargo.com/blog/2025/10/08/cpp26-range-support-for-std-optional
47•birdculture•5d ago•25 comments

A kernel stack use-after-free: Exploiting Nvidia's GPU Linux drivers

https://blog.quarkslab.com/./nvidia_gpu_kernel_vmalloc_exploit.html
92•mustache_kimono•5h ago•6 comments

Recreating the Canon Cat document interface

https://lab.alexanderobenauer.com/updates/the-jasper-report
56•tonyg•5h ago•1 comments

Reverse engineering a 27MHz RC toy communication using RTL SDR

https://nitrojacob.wordpress.com/2025/09/03/reverse-engineering-a-27mhz-rc-toy-communication-usin...
53•austinallegro•5h ago•10 comments

Garbage collection for Rust: The finalizer frontier

https://soft-dev.org/pubs/html/hughes_tratt__garbage_collection_for_rust_the_finalizer_frontier/
82•ltratt•7h ago•74 comments

Leaving serverless led to performance improvement and a simplified architecture

https://www.unkey.com/blog/serverless-exit
211•vednig•8h ago•148 comments

M5 MacBook Pro

https://www.apple.com/macbook-pro/
234•tambourine_man•6h ago•287 comments

Breaking "provably correct" Leftpad

https://lukeplant.me.uk/blog/posts/breaking-provably-correct-leftpad/
56•birdculture•1w ago•15 comments

I am sorry, but everyone is getting syntax highlighting wrong

https://tonsky.me/blog/syntax-highlighting/
9•robenkleene•44m ago•2 comments

Show HN: Scriber Pro – Offline AI transcription for macOS

https://scriberpro.cc/hn/
106•rezivor•7h ago•98 comments

Helpcare AI (YC F24) Is Hiring

1•hsial•7h ago

Americans' love of billiards paved the way for synthetic plastics

https://invention.si.edu/invention-stories/imitation-ivory-and-power-play
30•geox•6d ago•18 comments

Bots are getting good at mimicking engagement

https://joindatacops.com/resources/how-73-of-your-e-commerce-visitors-could-be-fake
299•simul007•8h ago•223 comments

Recursive Language Models (RLMs)

https://alexzhang13.github.io/blog/2025/rlm/
6•talhof8•2h ago•0 comments

Pixnapping Attack

https://www.pixnapping.com/
263•kevcampb•13h ago•61 comments

iPad Pro with M5 chip

https://www.apple.com/newsroom/2025/10/apple-introduces-the-powerful-new-ipad-pro-with-the-m5-chip/
169•chasingbrains•6h ago•196 comments

FSF announces Librephone project

https://www.fsf.org/news/librephone-project
1324•g-b-r•19h ago•532 comments

Just talk to it – A way of agentic engineering

https://steipete.me/posts/just-talk-to-it
140•freediver•13h ago•79 comments

Show HN: Specific (YC F25) – Build backends with specifications instead of code

https://specific.dev/
9•fabianlindfors•2h ago•0 comments

David Byrne Radio

https://www.davidbyrne.com/radio#filter=all&sortby=date:desc
74•bookofjoe•4h ago•17 comments