logoalt Hacker News

maccardyesterday at 7:08 PM2 repliesview on HN

I’m very much pro c++, but anti c++’s direction.

> optional is unsafe in idiomatic use cases? I’d like to challenge that.

    std::optional<int> x(std::nullopt);
    int val = *x;

Optional is by default unsafe - the above code is UB.

Replies

on_the_trainyesterday at 7:26 PM

But using the deref op is deliberately unsafe, and never used without a check in practice. This would neither pass a review, nor static analysis.

show 3 replies
TinkersWyesterday at 7:13 PM

That is actually memory safe, as null will always trigger access violation..

Anyway safety checked modes are sufficient for many programs, this article claims otherwise but then contradicts itself by showing that they caught most issues using .. safety checked modes.

show 3 replies