News for hackers. Stuff that matters.
Re:Why introduce `std::launder` rather than have the compile... by eps on Saturday January 22, 2022 @04:00AM (#30034973)
Oi vey. This sort of QA is exactly what's wrong with the "modern" C++.

It's just ugly.

I've been using C++ as my main language since it was barely out of C with Classes stage and it pains me to see how it evolved from a language that extended C in clever and welcomed ways into this piece of committee-designed monstrosity with not a trace of its former elegance left in it.

Re:Why introduce `std::launder` rather than have the compile... by snickerbockers on Saturday January 22, 2022 @04:07AM (#30035017)
It's absolutely disgusting how many of C++'s recent additions exist to work around problems introduced by C++'s other recent additions.
Re:Why introduce `std::launder` rather than have the compile... by bigcheesegs on Saturday January 22, 2022 @04:10AM (#30035034)
Do you have any examples? std::launder fixes an issue that has existed since C++98.
Re:Why introduce `std::launder` rather than have the compile... by tialaramex on Saturday January 22, 2022 @06:14AM (#30035687)
In C++ 03 they realise their pointer (inherited from C) is awful. Do they fix it? No, they add a new type named std::auto_ptr which is "smart". Thanks to operator overloading it can behave like the existing pointer. Kind of.

std::auto_ptr is pretty bad, and so in C++ 11 they deprecate auto_ptr and introduced std::shared_ptr and std::unique_ptr so now finally their language has halfway usable pointers, with these unwieldy names and some odd semantics you just have to get used to. C-style pointers remain all of: Simpler to use by virtue of their terse syntax, still necessary, and unsafe. std::auto_ptr still exists forever, but is officially deprecated.

At first C lacks strings, promoting string literals to a strange array with an extra zero value in it. Eventually C++ gets std::string and std::wstring but, nothing of value about these strings is defined, so they're both pretty useless and of course dangerous. In C++ 17 it gets std::string_view which finally expresses one of the things you probably first wanted, but it provides no enforcement of the most obvious expectation you have - C++ allows a string_view to outlive the underlying string, causing Undefined Behaviour. In C++ 20 you get to use ranged behaviour, and, that extra zero value bites you hard because of course you didn't mean the zero value but in C++ that's "part" of your string. They also, finally, decide that UTF-8 might be a good idea and define yet another type of string to mean UTF-8 encoded, about five years after everybody who matters was of course encoding strings as UTF-8.

Re:Why introduce `std::launder` rather than have the compile... by nemetroid on Saturday January 22, 2022 @06:36AM (#30035808)
> std::auto_ptr still exists forever, but is officially deprecated.

std::auto_ptr is fully removed since C++17.

I agree with most of your comment, but I think it reinforces the grandparent's point. The warts of std::string and null terminated strings are not "problems introduced by C++'s other recent additions".

Re:Why introduce `std::launder` rather than have the compile... by gpderetta on Saturday January 22, 2022 @09:58AM (#30037206)
Shared_ptr and unique_ptr are not replacement for raw C pointers. And the reason they are not blessed with language syntax is because user can and often do implement their own, so those two library types are not particularly special.
Re:Why introduce `std::launder` rather than have the compile... by BenFrantzDale on Saturday January 22, 2022 @10:18AM (#30037397)
Agreed. And a huge strength of C++ is that you can write low-level classes like these and have them be fully performant and have reasonable native-feeling syntax. Almost no other languages provide that.
Re:Why introduce `std::launder` rather than have the compile... by IiydAbITMvJkqKf on Saturday January 22, 2022 @11:53AM (#30038321)
Specifically, both shared_ptr and unique_ptr are owning pointers. Using plain old pointers for non-owning variables is idiomatic.
Re:Why introduce `std::launder` rather than have the compile... by tialaramex on Saturday January 22, 2022 @02:28PM (#30039990)
Non-owning pointers are a genuinely useful tool, but as a tool they're more welding torch than hammer, if that's what you needed it's really what you needed - but most often it probably isn't what you needed at all and will cause more harm than good.

So it's strange to provide them with stuff like terse built-in syntax, while not providing anything similar for the thing people will want all the time, ownership. Result: The Right Thing™ is harder to do and wastes visual space compared to the Wrong Thing. A "correct" program ends up full of boilerplate just to get what you'd obviously want.

Re:Why introduce `std::launder` rather than have the compile... by saagarjha on Saturday January 22, 2022 @10:34AM (#30037534)
> Eventually C++ gets std::string and std::wstring but, nothing of value about these strings is defined, so they're both pretty useless and of course dangerous. In C++ 17 it gets std::string_view which finally expresses one of the things you probably first wanted

Such as what? std::string_view represents exactly one thing, and that’s a slice into a string with a lifetime longer than it. That’s basically it. There aren’t really and other additional feature to it, so I feel like you may be confused as to what it does.

Re:Why introduce `std::launder` rather than have the compile... by tialaramex on Saturday January 22, 2022 @11:54AM (#30038327)
Yes, it's "just" a slice. Which is in fact one of the most obvious things you would want, and yet was not afforded by std::string.
Re:Why introduce `std::launder` rather than have the compile... by capitalsigma on Saturday January 22, 2022 @12:58PM (#30039069)
A lot of your criticism makes sense. I don't follow this one, though: `string_view` is *by definition` non-owning. Was non-owning the whole thing you wanted? If so, why are you surprised by lifetime issues? If non-owning isn't your main desire, what's wrong with `std::string`?
Re:Why introduce `std::launder` rather than have the compile... by tialaramex on Saturday January 22, 2022 @02:46PM (#30040162)
Probably in C++ context this feels unreasonable but I'm arguing that C++ is wrong here. It's OK for the language to enforce that the lifetime of the string underpinning a slice must equal or exceed the lifetime of the slice despite the slice not "owning" the string.

In 2011 there probably wasn't a mainstream non-GC language doing that. C++ would have been innovating here, and maybe C++ in 2011 didn't want to be innovating. By 2017 when C++ 17 introduces std::string_view it's behind the curve, this is not an exciting new feature, it's a weak imitation of what everybody else was already doing better.

Re:Why introduce `std::launder` rather than have the compile... by Razengan on Saturday January 22, 2022 @08:20AM (#30036376)
At this point the C probably stands for Cthulhu because the language borders on becoming a summoning ritual for eldritch horrors.
Re:Why introduce `std::launder` rather than have the compile... by thriftwy on Saturday January 22, 2022 @05:03AM (#30035297)
See also: https://yosefk.com/c++fqa/ as a long set of rants about this.
Re:Why introduce `std::launder` rather than have the compile... by teitoklien on Saturday January 22, 2022 @06:28AM (#30035766)
Whoever reads this , note that its a very outdated rant (although some parts are still true).

> Well, the standard library doesn't have matrices or regular expressions, either

C++ does provide a standard regex library now, matrices can be made either with vectors or you can use boost libs for it.

> The standard library doesn't support listing directories

Same for filesystems, it now comes with the standard set of libraries

For C++, it’s time and time again seen, that Boost libs are used as the experimentation stage to filter things and add to the C++ standard library, and boost is pretty feature rich with all sorts of utilities you might need to build a robust program.

> In C++, things are different - there are no modules.

C++20 is adding features related to modules support like in other programming languages (haskell, etc).

Sure C++ is an old programming language with too many weird features, which end up hurting devs , but as long as you stick to the fine features.

C++ is pretty fun to use. It has type inference now, those ugly looking long type-declarations, can easily be fixed by appropriate use of the “using” keyword in local scope (if you’re that worried about polluting the global namespace i.e)

I think C++ gets unnecessary hate , every language has its quirks, you just avoid the bad stuff and stick with the good bits.

Re:Why introduce `std::launder` rather than have the compile... by tsimionescu on Saturday January 22, 2022 @09:16AM (#30036781)
> I think C++ gets unnecessary hate , every language has its quirks, you just avoid the bad stuff and stick with the good bits.

C++ is the single most complex programming language ever devised. I don't think it deserves hate per se, but it there is no other language as hard to learn, as hard to write safe code for, and as hard to read.

Even your example of type inference is actually very hard to learn. If I do `auto x = 7;` will x be const or not? If I have `const int &y = 7; auto x = y;` will x be const int? int? const int&? What if it's a pointer, or volatile instead?

Re:Why introduce `std::launder` rather than have the compile... by teitoklien on Saturday January 22, 2022 @09:59AM (#30037224)
The problems you mentioned about type inference are valid, but this issue lies is any programming language which provides this feature, and a lot of languages which are promoted and looked upto on HN (like Haskell) heavily use type inference.

The simplest answer to your question of how to know what type the compiler will choose for type inferenced variables, is by compiling an example code snippet and printing out the type using either something like decltype or typeid (preferably the former).

But the thing is, the point of type inference is you want to abstract away the type, if you want to explicitly be aware of the type at all times, you should explicitly state it in declarations.

The issues described is inherent in the very notion of letting the compiler infer the type for you, which leads to this ambiguity.

If you want to avoid it, then best to be explicit, if you dont care about the type and are using typeinference to build compile time simple template functions using lambda and the type inferred parameters. Then type inference is pretty handy and leads to easy to read code.

It’s a tool, when you feel it’s getting in your way at writing code, you remove the tool/feature from your code.

Re:Why introduce `std::launder` rather than have the compile... by tsimionescu on Saturday January 22, 2022 @10:54AM (#30037698)
> but this issue lies is any programming language which provides this feature, and a lot of languages which are promoted and looked upto on HN (like Haskell) heavily use type inference.

> The issues described is inherent in the very notion of letting the compiler infer the type for you, which leads to this ambiguity.

It's not the same thing. Types and type qualifiers are different things, and few languages have type qualifiers in the same way as C++. In fact, auto, unlike type inference in any other language, does NOT match the full type of the variable - that's why things like `volatile auto &x = y` exist.

> But the thing is, the point of type inference is you want to abstract away the type

That's the point in other languages. In C++ it's much more just shorthand for the horribly long type names you end up with in templates.

Re:Why introduce `std::launder` rather than have the compile... by gpderetta on Saturday January 22, 2022 @01:11PM (#30039228)
Auto (and type deduction in general) is important in generic code.
Re:Why introduce `std::launder` rather than have the compile... by tsimionescu on Saturday January 22, 2022 @03:01PM (#30040305)
C++ had generic code for many years before auto (and decltype) existed. It definitely makes things easier, and I believe there are some types today that work with auto but can't be named otherwise. But, it's not directly required outside of these.

Template parameter inference, which mostly uses the same rules, is probably required in some SFINAE constructs, but is also not essential in other places.

Re:Why introduce `std::launder` rather than have the compile... by gpderetta on Saturday January 22, 2022 @10:00AM (#30037230)
For the most part auto follow the same rule as template argument deduction which is more than 20 years old.
Re:Why introduce `std::launder` rather than have the compile... by spoiler on Saturday January 22, 2022 @08:27PM (#30042740)
I don't think anyone hates C++, they hate how it makes them feel when they have to work in it.

Also, I think it's a bit ludicrous to say "just use a subset" because there's no definitive guide to "sane" or safe subset.

Also, regarding boost: I'm extremely conflicted about it... On one hand, it's made my life easier, but on the other it feels like once you add it to the codebase it metastases everywhere; it's not a C++ codebase anymote, but a boost codebase. Also, it ill-composes with most things that aren't boost (and sometimes even within other parts of boost, because boost is an amalgamation of libraries)

Re:Why introduce `std::launder` rather than have the compile... by xigoi on Sunday January 23, 2022 @02:01PM (#30049211)
It's 2002 and `import <iostream>;` still doesn't compile.