News for hackers. Stuff that matters.
Re:Modern C++ Won't Save Us by jandrewrogers on Monday April 22, 2019 @10:55PM (#19725221)
A significant issue I have with C++ is that even if your code base is pure C++17, the standard library is a Frankenstein's monster of legacy and modern C++ mixed together that required many compromises to be made. A standard library that usefully showed off the full capabilities of C++17 in a clean way would have to jettison a fair amount of backward compatibility in modern C++ environments.

I've noticed that more and more people like me have and use large alternative history "standard libraries" that add functionality, reimagine the design, and in some cases reimplement core components based on a modern C++ cleanroom. I've noticed that use of the standard library in code bases is shrinking as result. You can do a lot more with the language if you have a standard library that isn't shackled by its very long history.

Re:Modern C++ Won't Save Us by near on Tuesday April 23, 2019 @01:24AM (#19725810)
Because C++ is my primary language, and I always work on my codebases alone, I dropped the standard library and implemented my own replacement. It's not at all practical for most I'm sure, but it allows me to evolve the library with new revisions of the C++ standard without being absolutely fixed on backward compatibility.

One of the things I did for safety is that all access methods of all of my containers will bounds check and throw on null pointer dereferences ... in debug and stable mode. And all of that will be turned off in the optimized release mode, for applications where performance is absolutely critical. The consistency is very important.

Whenever I get a crash in a release mode, I can rebuild in debug mode and quickly find the issue. And for code that must be secure, I leave it in stable mode and pay the small performance penalty.

Re:Modern C++ Won't Save Us by masklinn on Tuesday April 23, 2019 @02:08AM (#19725995)
> A significant issue I have with C++ is that even if your code base is pure C++17, the standard library is a Frankenstein's monster of legacy and modern C++ mixed together that required many compromises to be made. A standard library that usefully showed off the full capabilities of C++17 in a clean way would have to jettison a fair amount of backward compatibility in modern C++ environments.

Not to mention C++ does not really provide the facilities necessary for convenient, memory-safe and fast APIs[0].

And as demonstrated by e.g. std::optional the standard will simply offer an API which is convenient, fast and unsafe (namely that you can just deref' an std::optional and it's UB if the optional is empty).

[0] I guess using lambdas hell of a lot more would be an option but that doesn't seem like the committee's style so far.

Re:Modern C++ Won't Save Us by jcelerier on Tuesday April 23, 2019 @03:20AM (#19726316)
> (namely that you can just deref' an std::optional and it's UB if the optional is empty).

if that was not the case, `optional` would get exactly zero usage. The point of those features is that you build in debug mode or with whatever your standard library's debug macro is to fuzz your code, but then don't inflict branches on every dereference for the release mode.

Re:Modern C++ Won't Save Us by masklinn on Tuesday April 23, 2019 @03:40AM (#19726394)
> The point of those features is that you build in debug mode or with whatever your standard library's debug macro is to fuzz your code, but then don't inflict branches on every dereference for the release mode.

That's completely insane. If there's always a value in your optional it has no reason to be an optional, if there may not be a value in your optional you must check for it.

Re:Modern C++ Won't Save Us by noselasd on Tuesday April 23, 2019 @04:23AM (#19726559)
Sure, but that's not the issue. You should be using a std::optional like e.g.

   if (my_optional) 
         do_stuff(*my_optional);
Here's one (explicit)conditional.

However if the dereferencing, *my_optional, should be safe, it too would need to perform a conditional check behind the scenes. But it doesn't - as C++ places that on the programmers hand to not sacrifice speed

Re:Modern C++ Won't Save Us by simias on Tuesday April 23, 2019 @06:10AM (#19727076)
This is solved in Rust by letting you test and unwrap at the same time:

    if let Some(obj) = my_optional {
        do_stuff(obj);
    }
>However if the dereferencing, my_optional, should be safe, it too would need to perform a conditional check behind the scenes. But it doesn't - as C++ places that on the programmers hand to not sacrifice speed

So basically that turns C++ optional types into fancy linter hints which won't actually improve the safety of the code much.

I understand C++'s philosophy of "you pay for what you use" but that's ridiculous, if you use an optional type it means that you expect that type to be nullable. Having to pay for a check is "paying for what you use". If you don't like it then don't make the object nullable in the first place and save yourself the test. That's just optimizing the wrong part of the problem.

Re:Modern C++ Won't Save Us by maccard on Tuesday April 23, 2019 @09:27AM (#19728330)
You can also do it in a one-liner in C++ if you're using shared pointers:

    if(auto obj = my_weak_ptr.lock())
    {
        do_stuff(obj);
    }
> Having to pay for a check is "paying for what you use". If you don't like it then don't make the object nullable in the first place and save yourself the test.

The point is that I can choose _when_ to pay that cost (e.g. I can eat the nullability check at this point, but not at this point, and I can use more sophisticated tooling like a static analyser to reason that the null check is done correctly).

Is it more error prone? yes. Does it allow for things to horribly wrong? yes. Is "rewriting it in rust" a solution? No. If I want to pay the cost of ref-counting, I can use shared/weak ptrs.

Re:Modern C++ Won't Save Us by aliceryhl on Tuesday April 23, 2019 @11:10AM (#19729379)
The rust code in question is not using reference counting.
Re:Modern C++ Won't Save Us by cpeterso on Tuesday April 23, 2019 @06:41PM (#19733715)
Rust's borrow checker is like compile-time reference counting. Same benefit, but no run-time cost.
Re:Modern C++ Won't Save Us by masklinn on Tuesday April 23, 2019 @06:30AM (#19727169)
> So basically that turns C++ optional types into fancy linter hints which won't actually improve the safety of the code much.

C++'s optionals are less "safer pointers" and more "stack-allocated pointers" (nor to be confused with pointers to stack allocations).

Re:Modern C++ Won't Save Us by noselasd on Thursday April 25, 2019 @02:47PM (#19751296)
C++ gives you all the options as usual.

  do_stuff(my_optional.value())
Is also safe, it throws if the value is absent, the safety check is performed behind the scenes.

But people might not want to throw an exception, so

  if (my_optional) 
         do_stuff(*my_optional);
Must also be allowed. The consequence is someone can also just do

  do_stuff(*my_optional)
No safety check is done and you get undefined behavior if the value is absent.

I don't know rust so I suspect it has a language construct which c++ lacks that prevents you from doing

  let Some(obj) = my_optional 
  do_stuff(obj);
Re:Modern C++ Won't Save Us by steveklabnik on Thursday April 25, 2019 @03:53PM (#19751865)
Yes, you have to use if let, not let. That code would be a compiler error. (Specifically, a “non-exhaustive pattern” error.)
Re:Modern C++ Won't Save Us by masklinn on Tuesday April 23, 2019 @05:43AM (#19726926)
Hence going back to the original issue I pointed:

> C++ does not really provide the facilities necessary for convenient, memory-safe and fast APIs.

> You should be using a std::optional like e.g. […] if the dereferencing, *my_optional, should be safe

And once again a terrible API puts the onus back on the user to act like a computer.

Re:Modern C++ Won't Save Us by comex on Tuesday April 23, 2019 @04:45AM (#19726644)
Nah. In simple cases like that, the compiler would always be able to optimize away an extra check, if such a check were present. After inlining operator bool and operator *, it would look something like

    if (my_optional->_has_value)
        if (my_optional->_has_value)
            do_stuff(my_optional->_value);
        else
            panic();
 
and the compiler knows that the second if statement will pass iff the first does.

On the other hand, if the test is further away from the dereference, and perhaps the optional is accessed through a pointer and the compiler can't prove it doesn't alias something else, it might not be able to optimize away the check. However, that probably doesn't account for too high a fraction of uses.

Re:Modern C++ Won't Save Us by ayosec on Tuesday April 23, 2019 @04:47AM (#19726650)
How is that different to this?

    if (my_pointer != NULL)
        do_stuff(*my_pointer)
Re:Modern C++ Won't Save Us by humanrebar on Tuesday April 23, 2019 @07:16AM (#19727415)
Native pointers do a bunch of different things depending on the context. In contrast, optional has clear semantics.

For instance, the ++ operator doesn't work for std::optional. For a native pointer, you just have to know (how?) not to use it.

Re:Modern C++ Won't Save Us by w0utert on Tuesday April 23, 2019 @07:07AM (#19727371)
In terms of generated coded, it is exactly the same. But that's not the point of optional types.

The point of optional types is to force you to write checks for undefined values, otherwise your code will not compile at all. In the old-fashioned style of your example, you might forget to check for the possibility of a null pointer/otherwise undefined value, and use it as if it were valid.

Re:Modern C++ Won't Save Us by 0xffff2 on Tuesday April 23, 2019 @01:04PM (#19730569)
But the whole genesis for this comment chain is that you can make exactly the same mistake with std::optional.
Re:Modern C++ Won't Save Us by w0utert on Sunday April 28, 2019 @08:09AM (#19770674)
Only if you deliberately unwrap the optional, which means you either don’t know what you are doing (in which case no programming language feature will be able to save you), or that you’ve considered your options and decided you want to enter the block knowing some variable can be undefined.

IMO, that’s not the same as not having optionals at all, and writing unconditional blocks left and right that may or may not operate on undefined values. It super easy to just dereference any pointer you got back from some function call in C++, without paying attention. Optionals force you to either skip the blocks, or think about how to write them to handle undefineds. Also, it’s ‘code as documentation’ in some sense, which I’m a big proponent of.

Re:Modern C++ Won't Save Us by masklinn on Wednesday May 08, 2019 @05:29AM (#19857223)
> Only if you deliberately unwrap the optional

"Deliberately unwrap the optional" is the exact same thing as "deliberately unwrap the pointer", you just deref' it and it's UB if the optional / pointer is empty.

C++'s std::optional is not a safety feature (it's no safer than using a unique_ptr or raw pointer), it's an optimisation device: if you put a value in an std::optional you don't have to heap allocate it.

> It super easy to just dereference any pointer you got back from some function call in C++, without paying attention.

And optionals work the exact same way. There's no difference, they don't warn you and they don't require using a specific API like `value_unchecked()`. You just deref' the optional to get the internal value, with the same effects as doing so on any other pointer.

Re:Modern C++ Won't Save Us by millstone on Monday April 22, 2019 @11:43PM (#19725415)
I agree with this and would take it a step further, and say that recent changes to the STL are the worst parts of modern C++. For example std::regex supports 6 distinct syntaxes, the PRNG stuff is massively over-engineered, the "extensions for parallelism" add complexity without giving enough knobs for any real perf improvement. Meanwhile there's gaping holes like UTF-8 support. It's a sad state.
Re:Modern C++ Won't Save Us by blt on Tuesday April 23, 2019 @12:56AM (#19725706)
How is the prng over engineered? I agree its a little clunky for casual use but it makes all the right decisions, imo, for serious use of prngs (e.g. reproducible experiments for Monte Carlo methods in simulation and statistics)
Re:Modern C++ Won't Save Us by petters on Tuesday April 23, 2019 @01:27AM (#19725816)
Initializing the mersenne twister is really hard: https://github.com/PetterS/monolith/blob/master/minimum/core...

Edit: There are two links in the code with more info.

Re:Modern C++ Won't Save Us by netheril96 on Tuesday April 23, 2019 @03:11AM (#19726270)
Most other random libraries, in whatever languages, do not even offer the option of this "right" version of initialization. They all just seed with a 32-bit integer. If you are content with those libraries, you should be content with a simple `std::mt19937{std::random_device{}()}`.
Re:Modern C++ Won't Save Us by whyever on Tuesday April 23, 2019 @11:33AM (#19729611)
I don't think this is true for modern languages.

Rust seeds the whole state by default: https://docs.rs/rand/0.6.5/rand/

Julia seems to seed 128 bits: https://github.com/JuliaLang/julia/blob/5741c7f53c5ea443bbd7...

However, your statement seems to apply to old languages:

C# uses only 32 bits and the time for seeding: https://docs.microsoft.com/en-us/dotnet/api/system.random?vi...

Java only supports 48 bit seeds: https://docs.oracle.com/javase/8/docs/api/java/util/Random.h...

Re:Modern C++ Won't Save Us by dearrifling on Tuesday April 23, 2019 @02:04AM (#19725974)
One shouldn't std::move in a return. Returning a local is already automatically an rvalue, however explicitly moving it disables copy elision.
Re:Modern C++ Won't Save Us by petters on Tuesday April 23, 2019 @02:22AM (#19726077)
You are completely right. That should be fixed
Re:Modern C++ Won't Save Us by madisfun on Tuesday April 23, 2019 @04:31AM (#19726592)
Mersenne Twister has a huge state, and if you have to use the MT (you need a very long period), you will probably want to be able to initialize it properly. For the common use case is a linear congruential generator which is initialized with just one integer.
Re:Modern C++ Won't Save Us by GoblinSlayer on Tuesday April 23, 2019 @11:51AM (#19729778)
Huh? You can't possibly need a period 2^19937-1.
Re:Modern C++ Won't Save Us by rcxdude on Tuesday April 23, 2019 @06:54AM (#19727292)
The problem is there's no easy to use sensible defaults, just a confusing bunch of options with a bunch of apparently easy but subtely wrong ways to use it. Having the power is useful, but I would also just like a rand() (or better a randrange()) which actually works.
Re:Modern C++ Won't Save Us by patrec on Tuesday April 23, 2019 @06:48AM (#19727267)
> it makes all the right decisions, imo, for serious use of prngs

Apart from an awkward API that's hard to use correctly, Mersenne Twister which is basically the main generator has been obsolete for years (bad quality RNs, slow, huge state, ...).

Re:Modern C++ Won't Save Us by sorenjan on Tuesday April 23, 2019 @08:29AM (#19727897)
What's the modern C++ equivalent to C's

    (rand() % (b - a)) + a;
or Python's

    random.randint(a, b)
Easy to use and often good enough.
Re:Modern C++ Won't Save Us by whyever on Tuesday April 23, 2019 @11:14AM (#19729416)
> (rand() % (b - a)) + a;

This is no longer uniform, because it introduces a bias towards small numbers.

Re:Modern C++ Won't Save Us by sorenjan on Tuesday April 23, 2019 @12:49PM (#19730396)
Yes, it's less than ideal. But like I said, often good enough. Sometimes you just want a simple way to get something approximately random, the actual distribution might be unimportant.
Re:Modern C++ Won't Save Us by colanderman on Monday April 22, 2019 @11:36PM (#19725381)
What parts specifically? By my estimation, the only non-deprecated part of the standard library that really reeks of pre-C++11 (what I believe most consider the advent of "modern") is iostream. Most of e.g. the containers have been kept up to date with new features of the language (e.g. move semantics, constexpr).

The standard library certainly is lacking things which are commonly used (say, JSON parsing or database connection), but I think this is a conscious decision (and IMO the correct decision) to include only elements that have a somewhat settled, "obvious", lowest-common-denominator semantics. There's rhyme and reason to most of the most commonly used elements that is decidedly lacking from e.g. Python's (much more extensive) standard library.

Re:Modern C++ Won't Save Us by geezerjay on Tuesday April 23, 2019 @01:28AM (#19725821)
> The standard library certainly is lacking things which are commonly used (say, JSON parsing or database connection),

I strongly disagree. It's quite obvious that the C++ standard library does not need to add support for "common things", because they already exist as third-party modules.

In fact, this obsession to add all sorts of cruft to the C++ standard is the reason we're having this discussion.

If there is no widely adopted JSON or DB library for C++ then who in their right mind would believe it would be a good idea to force one into the standard?

And don't get me started on the sheer lunacy of the proposal to add a GUI library. Talk about a brain-dead idea.

People working on other programming language stacks already learned this lesson a lot of time ago. There's the core language and there's the never-ending pile of third-party modules. Some are well-made and well thought-out, others aren't. That doesn't matter, because these can be replaced whenever anyone feels like it. This is not the case if a poorly thought-out component is added to an ISO standard.

Re:Modern C++ Won't Save Us by barrkel on Tuesday April 23, 2019 @03:32AM (#19726356)
Standard libraries shouldn't include "leaf" modules, but probably should include interface / adapter modules. So no to JSON, but maybe yes to a serde interface. No to a database driver, but maybe yes to an interface like JDBC.

Without common interfaces, flexibility in implementation is much more expensive, and innovation suffers too, as new things are harder to get off the ground without existing code that they can cheaply plug into.

Re:Modern C++ Won't Save Us by daemin on Tuesday April 23, 2019 @07:49AM (#19727639)
There's an argument to be made for having basic so-called leaf modules in the standard library. That is it makes it far simpler to get a basic installation of C++ and start doing cool things with it. Experienced developers or people that need domain specific features would be using their own specialised libraries anyway.

So instead of trying to figure out which one of the dozens of GUI frameworks to use in making a window and have it change colour, you just write it using the standard library. You want to do a HTTP request, then there will be code in the standard library for that.

It will also save work trying to figure out which third party library to use when you want to do these things locally on a small test project.

Re:Modern C++ Won't Save Us by geezerjay on Wednesday April 24, 2019 @02:01AM (#19735923)
> So instead of trying to figure out which one of the dozens of GUI frameworks to use in making a window and have it change colour, you just write it using the standard library.

Congrats, now you're stuck with something like Xwindows or MFC or AWT.

Re:Modern C++ Won't Save Us by daemin on Wednesday April 24, 2019 @05:45AM (#19736860)
They're standard for the OS but not a standard for the programming language library.

Granted AWT wasn't great but you could still make a GUI with it straight out of the box. It allowed you to make windows and buttons and start exploring the programming language.

Like I said having a standard library option won't eliminate third party libraries, it will just provide something in the box for people to start using straight away.

Re:Modern C++ Won't Save Us by pjmlp on Wednesday April 24, 2019 @04:01AM (#19736435)
Which while not ideal, are guaranteed to be present, contrary to third party libs.
Re:Modern C++ Won't Save Us by geezerjay on Wednesday April 24, 2019 @10:47AM (#19738860)
That assertion is disingenuous at best.

That guarantee is only achievable at the expense of forcing compiler developers to maintain a GUI toolkig for all platforms. Who in their right mind believes that's reasonable or desirable?

Re:Modern C++ Won't Save Us by pjmlp on Wednesday April 24, 2019 @12:15PM (#19739901)
Everyone that wants a language to thrive instead of dealing with thousand incompatible implementations.

Many C++ targets don't support IO or networking, so lets not burden embedded compiler developers with standard library bloat.

Re:Modern C++ Won't Save Us by daemin on Wednesday April 24, 2019 @12:05PM (#19739795)
So instead of a GUI library what about a HTTP or a network library as part of the standard? Surely handling TCP and UDP connections is an order of magnitude easier to implement and maintain.
Re:Modern C++ Won't Save Us by coldtea on Tuesday April 23, 2019 @05:04AM (#19726740)
>I strongly disagree. It's quite obvious that the C++ standard library does not need to add support for "common things", because they already exist as third-party modules.

It's not obvious to me at all.

In fact, if that was a valid argument, it would be for C++ not having a standard library at all, as everything (including vectors, strings, etc) also exists as "third-party modules".

Re:Modern C++ Won't Save Us by geezerjay on Tuesday April 23, 2019 @01:50PM (#19731054)
> In fact, if that was a valid argument, it would be for C++ not having a standard library at all

Putting aside the continuum fallacy, it's easy to understand how the C++ would be better served by having access to a collection of third-party components instead of repeating C's and even Java's mistakes.

The Boost project is a very good example, so as the wealth of JSON and XML parsers.

In fact, this lesson is so blatantly obvious that essentially all mainstream programming languages simply adopt official package managers and leave it to the community to develop and adop the components they prefer.

Re:Modern C++ Won't Save Us by coldtea on Wednesday April 24, 2019 @03:25AM (#19736286)
>Putting aside the continuum fallacy, it's easy to understand how the C++ would be better served by having access to a collection of third-party components instead of repeating C's and even Java's mistakes.

Java is very well served with its library. It would have been nowhere near as successful without it.

Re:Modern C++ Won't Save Us by humanrebar on Tuesday April 23, 2019 @07:21AM (#19727436)
Third party modules would be a huge mess if there weren't at least common interface types like std::string_view and std::unique_ptr.
Re:Modern C++ Won't Save Us by adrianN on Tuesday April 23, 2019 @03:41AM (#19726399)
I believe that C++ needs a fat standard library because using third party libraries is a bit cumbersome in C++. Alternatively there could be a blessed build system that makes third party library integration as easy as Cargo or Go Modules.
Re:Modern C++ Won't Save Us by Maken on Tuesday April 23, 2019 @04:19AM (#19726545)
I would say that CMake pretty much covers that. What it lacks is somehow a central registry, but I think C++ never intended to have one.
Re:Modern C++ Won't Save Us by coldtea on Tuesday April 23, 2019 @05:05AM (#19726741)
What C++ intended and what C++ should have intended are two different things.
Re:Modern C++ Won't Save Us by colanderman on Tuesday April 23, 2019 @09:16AM (#19728255)
> I strongly disagree

No you don't. Read the rest of the sentence you quoted :)

Re:Modern C++ Won't Save Us by gpvos on Tuesday April 23, 2019 @05:26AM (#19726828)
You're not disagreeing with colanderman:

> (and IMO the correct decision)

Re:Modern C++ Won't Save Us by Althorion on Tuesday April 23, 2019 @07:15AM (#19727413)
> include only elements that have a somewhat settled, "obvious", lowest-common-denominator semantics

Can you, from the top of your head, tell me what irregular modified cylindrical Bessel functions are and the last time you needed to use one? And yet, they were included in the standard library in C++17: https://en.cppreference.com/w/cpp/numeric/special_math/cyl_b...

Re:Modern C++ Won't Save Us by colanderman on Tuesday April 23, 2019 @09:15AM (#19728242)
I can't, but I bet they have a standard and well-accepted definition in the mathematical community.

In fact, pretty much any real-valued mathematical function passes the test.

The interface is settled, almost by definition since C++ functions are inspired by mathematical functions: pass in arguments, return result. Use range/domain exceptions or NaN for reporting such errors.

The semantics are obvious: compute the named function.

The interface is lowest-common-denominator: include float, double, and long double overloads.

In fact, the same or similar interface is used in almost every language I've encountered. To contrast, the same is absolutely not true of e.g. a database module. I don't think I've ever seen two alike, disagreeing over even basic things such as whether the cursor or the transaction is the basic unit of interaction.

Re:Modern C++ Won't Save Us by Althorion on Tuesday April 23, 2019 @09:32AM (#19728370)
There’s nearly a limitless amount of standard and well-defined functions with a single usage, like those. There’s hardly a point in implementing them in the standard library and C++ is the only language that I’m aware of that has those.

If the goal was to create a specialized library for solving differential equations, those would be handy there. But if not, even if you tried implementing everything that you could potentially think of to implement, there are hundreds of things that are orders of magnitude more useful to have and equally well-defined and standardized—even if we limit ourselves to mathematics alone, I’d much rather see basic constants like π or e included, or quaternions, or arbitrary precision integers, or decimal numbers… or dozens upon dozens of other things before that.

But mainly, I find it impossible to maintain the claim that any general-usage language, like C++, that implements such niche functions is trying to keep its standard library small and ‘include only elements that have a somewhat settled, "obvious", lowest-common-denominator semantics’.

Re:Modern C++ Won't Save Us by colanderman on Tuesday April 23, 2019 @09:44AM (#19728476)
Then you're not disagreeing with me, because those functions pass my test as I demonstrated above.

Why do these functions bother you so much? It can't be namespace pollution; they're under std::. It can't be that you disagree with their interface or semantics, since by your own admission you don't even know what they are.

You named some other features, such as quaternions, that you think would be better for implementors to spend their time on, but surely you can imagine someone like yourself who is tired of having to define the Bessel functions every time they start a new project, and can't imagine why the C++ committee saw fit to include something so useless and obscure as quaternions before getting to Bessel functions.

Re:Modern C++ Won't Save Us by Althorion on Tuesday April 23, 2019 @10:33AM (#19728991)
> Then you're not disagreeing with me, because those functions pass my test as I demonstrated above.

Yeah. I must have misunderstood your definition of ‘obvious’—I though you meant ‘an obvious inclusion to the standard library’, not ‘having an obvious definition’. The definition is obvious, why they should be in a standard library is not.

> […] since by your own admission you don't even know what they are

I mostly do—I studied mathematics. Or, to be more precise, I learned about them, then never used them in programming, had to remind myself what they were and even after that, I don’t find them useful enough to warrant an inclusion to the standard library. Thus, since they were included, I think that’s a good evidence of C++ committee not trying to keep its standard library concise.

> You named some other features, such as quaternions, that you think would be better for implementors to spend their time on, but surely you can imagine someone like yourself who is tired of having to define the Bessel functions every time they start a new project, and can't imagine why the C++ committee saw fit to include something so useless and obscure as quaternions before getting to Bessel functions.

The thing is, I can’t. If you use them, you want a better support for solving differential equations than C++ offers anyway, so it’s more of a ‘OK, I have this small part already implemented, but I still have to find ways of doing the rest 95%’. This, plus the fact that I’m quite certain that people using C++ to do 3D geometry outnumber people using it for solving differential equations by a few orders of magnitude—a cursory glance at GitHub showed me that the only projects in C++ that mention it are… implementations of a standard library (and forks upon forks of those).

My problems with this is that C++ is now in a very strange place—it implements some very high level, niche features, bloating the language and its implementations (the size of glibc is a practical problem) while still lacking many others, that seem much more ‘obvious’ (i.e. ‘if given an unknown language, I would be much less surprised to find them included in its standard library). In the end, I have a language that both has annoyingly big standard library and heavily relies on other, non-standard ones for quite a lot of things.

Re:Modern C++ Won't Save Us by majewsky on Tuesday April 23, 2019 @10:35AM (#19729008)
> C++ is the only language that I’m aware of that has those.

https://golang.org/pkg/math/#J0

Re:Modern C++ Won't Save Us by int_19h on Tuesday April 23, 2019 @03:24PM (#19731974)
Surely JSON does have a settled, obvious, lowest-common-denominator semantics?
Re:Modern C++ Won't Save Us by colanderman on Wednesday April 24, 2019 @12:13AM (#19735502)
Of the design of a parsing and encoding library? Not at all. Do you parse as a stream or all in one go? Are values represented as a special "json" type, or as built-in types? How should arrays and objects be represented? Are integers and reals different types? Are trailing zero decimals significant? Do you allow construction of arrays and objects in any order, or only sequentially?

(Granted, I've written my own C++ JSON library which I believe answers all these questions in an intuitive way, following both the design principles of the C++ standard library, and the lowest-common-denominator semantics of JSON, but it's sufficiently opinionated that I doubt I could convince any significant portion of C++ users that it's the "right" way to do things. Even if it "is", demonstrating such is nowhere near as easy as it is for unique_ptr, vector, string, thread, etc., each of which are more or less the "obvious" designs given certain constraints such as RAII to which the standard library adheres.)

Re:Modern C++ Won't Save Us by kabdib on Tuesday April 23, 2019 @04:43AM (#19726635)
I work in a shop where there was a significant effort in a cross-platform library a long time ago, but that old code has been showing cracks and emitting creaks ("Hey, folks, guess how many debugging hours it took to find out that lambdas didn't work here, either"). Use of the standard library is frowned upon except when absolutely necessary, so there's no avoiding the thing. From time to time someone will joust at it and pull a particularly screwball section forward a decade or two, but on the whole the old stuff is just never going away short of a catastrophe. It makes onboarding interesting, and it makes you reflect philosophically on expertise that is valuable absolutely no place else.

I work on other projects, or on my own stuff at home, and I can breathe again. I don't always need reverse iterators on a deque, but dammit they are there if I need them.

However, I have been in too much C runtime code to be entirely happy. I've seen too many super-complicated disasters, for instance the someone who really wanted to write the Great American OS Kernel but who wasn't allowed on the team, and so had to make their bid for greatness in stdio.h instead. You learned to tread carefully in that stuff, the only good news being that if you broke something it might have turned out to be already busted anyway and no harm done, philosophically speaking, I mean.

There are no good answers :-)

Re:Modern C++ Won't Save Us by fsloth on Tuesday April 23, 2019 @01:33AM (#19725852)
So the language is evolving, it is used by projects that are old and still in good enough shape so one can adapt their concepts to some new things, and as a sugar on top, it does not break bakcwards compatibility.

As such it just sounds like a mature technology which a huge adopted base and is still holding traction. Generally maturity, traction and adaptability can be considered indicators of health and not malady.

Beauty is overstated. Engineering can be art but it doesn't have to be.

Jokes aside, I use C++ daily and see it as Warty McWartface and could spend a long time ruminating about it's faults. But adapting old stuff to new boundaries is always going to be messy. Generally rewriting history creates more problems than solves them.

Re:Modern C++ Won't Save Us by fooker on Monday April 22, 2019 @11:51PM (#19725471)
I don't see the problem. You are free to use such a modern library (Google does, it's called absl).

The good thing here is that the standard library doesn't require 'magic' to be implemented (unlike Swift where the standard library relies on hidden language hacks).

Re:Modern C++ Won't Save Us by nimrody on Tuesday April 23, 2019 @03:08AM (#19726251)
The difficulty here is combining multiple libraries each using its own abstractions.

For example, since the standard library does not have a Matrix class suitable for numerical applications (or maybe it does today...) using multiple libraries each with its own Matrix class is difficult. Multiple libraries are needed since one library may not contain all numerical algorithms one may require for a given app.

This is not a problem for Google where I assume everyone is using internally written code -- but is a problem for most of us.

Re:Modern C++ Won't Save Us by jcelerier on Tuesday April 23, 2019 @03:25AM (#19726333)
> For example, since the standard library does not have a Matrix class suitable for numerical applications (or maybe it does today...) using multiple libraries each with its own Matrix class is difficult.

well, Python comes with a builtin "matrix-like" array type and yet it's not the one which is the most used in scientific computation.

Re:Modern C++ Won't Save Us by aldanor on Tuesday April 23, 2019 @07:55AM (#19727675)
Python provides the buffer interface, however (which `array` module implements), which links Python's buffers and memoryviews to numpy arrays to multiple other 3rd-party array-like and table-like types and structures.
Re:Modern C++ Won't Save Us by RayDonnelly on Tuesday April 23, 2019 @07:50AM (#19727642)
.. because it's (relatively speaking) brand new?
Re:Modern C++ Won't Save Us by nradov on Tuesday April 23, 2019 @12:04AM (#19725532)
Sure it's possible to use a non-standard "standard library". But at that point you're already halfway to using a different language so why not consider switching from C++ to D / Rust / Go?
Re:Modern C++ Won't Save Us by ncmncm on Tuesday April 23, 2019 @12:50AM (#19725692)
The whole point of C++ is that it enables writing more powerful libraries, capturing semantics in libraries that can then just be used. C++ is still quite a lot more powerful for this purpose than Rust. Rust will get better at it, over time, but it has a long way to go and C++ is not siiting still.

Rust is still a niche language, and if its rates of adoption and improvement do not keep up, it will remain a niche language, and fade away like Ada.

I cannot imagine a serious programmer switching from C++ to Go. If you can, you have a much livelier imaginary life than I do.

Re:Modern C++ Won't Save Us by ajxs on Tuesday April 23, 2019 @01:57AM (#19725950)
The Ada partisans are all out in force here in this thread to defend Ada, all four of us. haha... For what it's worth, niche as Ada may be, it's an _important_ niche. It remains widespread in safety-critical applications, and isn't going anywhere anytime soon. It's really good to see Rust taking lessons from Ada/SPARK in the area of formal proofing! If any language is going to threaten C++, it looks like Rust. I don't expect an Ada resurgence to happen, unfortunately.

> I cannot imagine a serious programmer switching from C++ to Go. If you can, you have a much livelier imaginary life than I do.

This got a laugh out of me.

Re:Modern C++ Won't Save Us by pjmlp on Tuesday April 23, 2019 @02:05AM (#19725984)
A large majority of Ada partisans found their corner in Java, C# and C++'s type system improvements over C, and made the best we could from the sour grapes of C's copy-paste compatibility.
Re:Modern C++ Won't Save Us by ajxs on Tuesday April 23, 2019 @02:18AM (#19726058)
Calling myself an Ada partisan is a bit of a stretch. I've recently begun using it for embedded development, which is a domain almost completely dominated by C. That's the angle I'm coming in from.
Re:Modern C++ Won't Save Us by pjmlp on Tuesday April 23, 2019 @02:40AM (#19726152)
It depends on each one naturally.

For me, coming from Turbo Pascal 3 - 6, it allowed me in 1993 to use a language with a similar level of safety and language features, instead of having to deal with PDP-11 constraints.

I was always the weird kid that asked the professors if I could deliver C projects written in C++ instead, which thankfully some of them did accept.

Specially given that at my degree, C was already out of fashion by the early 90's. First year students got to learn Pascal and C++, and were expected to learn C from their introduction to C++ programming.

Re:Modern C++ Won't Save Us by nicoburns on Tuesday April 23, 2019 @07:11AM (#19727392)
> Rust is still a niche language

Only just barely at this point. It has significant projects from a lot of the largest companies (Google, Microsoft, Amazon, etc). Firefox is using it, Dropbox is using it, Red Hat is using it.

Re:Modern C++ Won't Save Us by ncmncm on Tuesday April 23, 2019 @10:32AM (#19728978)
In five years it might be just barely a niche language. In ten, if it catches on, it won't be.

If it does, its users will have come over from Java, C#, and C.

Re:Modern C++ Won't Save Us by rootlocus on Tuesday April 23, 2019 @02:15AM (#19726042)
Is this the "no serious programmer" fallacy?
Re:Modern C++ Won't Save Us by Y_Y on Tuesday April 23, 2019 @03:21AM (#19726317)
No true hn commenter would make such a mistake.
Re:Modern C++ Won't Save Us by machinecoffee on Tuesday April 23, 2019 @07:19AM (#19727427)
From what I can see, the whole point of C++ is to wrap existing C libraries and call them OO :-)
Re:Modern C++ Won't Save Us by fooker on Tuesday April 23, 2019 @12:09AM (#19725548)
No, using a library is not halfway to using a different language.

Languages exist to allow you to define your own layers of abstractions. The language choice ideally reflects what abstractions are useful for your project.

Re:Modern C++ Won't Save Us by geezerjay on Tuesday April 23, 2019 @01:36AM (#19725866)
> But at that point you're already halfway to using a different language

This statement makes no sense at all. Using a third-party library that's not specified by the same ISO standard that specifies the core languagr does not create "a different language".

It just means you're actually using the programming language to do stuff.

This isn't the case even if someone uses a toolkit that relies on preprocessor tricks to give the illusion of extending the core language, such as Qt.

Re:Modern C++ Won't Save Us by mempko on Tuesday April 23, 2019 @12:32AM (#19725635)
C++ is designed for people to make nice libraries. Unlike other languages there is nothing special about the standard library (no magic language hacks). All libraries are first class citizens by design.
Re:Modern C++ Won't Save Us by usrnm on Tuesday April 23, 2019 @01:00AM (#19725721)
Good luck implementing something like std::is_standard_layout without "magic language hacks". No, not all libraries are made equal and std is part of the language now, there is no way back
Re:Modern C++ Won't Save Us by geezerjay on Tuesday April 23, 2019 @01:47AM (#19725904)
You've cherry-picked a type trait as your example, which arguably could be a core language feature made to look like a third-party module.

Meanwhile, do you believe it's hard to implement a container?

And no, adding cruft to the STL is not a one-way street. See for example the history of C++'s smart pointers.

Re:Modern C++ Won't Save Us by int_19h on Tuesday April 23, 2019 @03:02PM (#19731729)
It is pretty hard to implement a container with all the precise invariants and guarantees that the Standard requires.

But more to the point, your implementation might still not be as fast as the standard library one, because the standard library can make assumptions about the compiler that you cannot in portable code - what is UB to you might be well-defined behavior to stdlib authors. Thus, for example, they might be able to use memcpy for containers of stdlib types that they know are safe to handle in that manner.

Re:Modern C++ Won't Save Us by fooker on Tuesday April 23, 2019 @04:50AM (#19726671)
A look into the type_traits header reveals that is_standard_layout is implemented with standard C++.
Re:Modern C++ Won't Save Us by Kranar on Tuesday April 23, 2019 @08:37AM (#19727960)
Re:Modern C++ Won't Save Us by fooker on Tuesday April 23, 2019 @09:08AM (#19728191)
It checks if a non standard feature is available, otherwise falls back to a standard implementation.

My argument was that it was possible to implement it with standard C++.

Re:Modern C++ Won't Save Us by Kranar on Tuesday April 23, 2019 @01:54PM (#19731093)
There's no way to implement that type trait using standard C++. The implementation does check if a non-standard feature is available, and if not it delegates to is_scalar which in turn delegates to is_enum which in turn delegates to is_union. is_union can not be implemented in a standard conforming manner without compiler support and libc++ unconditionally returns false if compiler support is not available, which does not conform to the standard.
Re:Modern C++ Won't Save Us by pjmlp on Tuesday April 23, 2019 @02:03AM (#19725972)
Lack of tooling for HPC, GPGPU, mixed language graphical debugging across Java and .NET, native support for COM/UWP, game engines like Unreal, CryEngine and Unity.
Re:Modern C++ Won't Save Us by Const-me on Tuesday April 23, 2019 @09:55AM (#19728574)
Many libraries only expose C or C++ APIs. Some of these libraries are hard requirement, like OS kernel APIs or GPU APIs.

Insufficient SIMD support in other languages, Intel only supports their intrinsics in C and Fortran.

Tools for C++ are just too good, IDEs, debuggers, profilers.

Re:Modern C++ Won't Save Us by Razengan on Tuesday April 23, 2019 @02:05AM (#19725980)
What are the hidden language hacks in Swift?
Re:Modern C++ Won't Save Us by Someone on Tuesday April 23, 2019 @03:38AM (#19726384)
I wouldn’t call them hacks, but there are things in the runtime that you can’t implement yourself in Swift. Examples (corrections welcome):

- you can’t allocate memory and then turn it into an Swift object.

- you can’t write Decodable in pure Swift (reflection isn’t powerful enough to do “set the field named “foo” in this structure to “bar”)

- reference counts are hidden from Swift code (yes, there’s swift_retainCount to read them, but that’s documented as returning a random number (https://github.com/apple/swift/blob/master/docs/Runtime.md) because it should not be used). So, if the compiler emits more reference count logic than needed in the data structure that your library uses, there’s no way to improve on it.

Re:Modern C++ Won't Save Us by fooker on Tuesday April 23, 2019 @03:59AM (#19726476)
There are a lot more of these, I can't find a comprehensive list unfortunately.
Re:Modern C++ Won't Save Us by rimliu on Tuesday April 23, 2019 @03:34AM (#19726370)
What are those "hidden language hacks" in Swift?
Re:Modern C++ Won't Save Us by hak8or on Monday April 22, 2019 @11:33PM (#19725369)
Can you or others post such alternative standard libraries? The only ones that come to mind are boost (which is a nightmarefor compile times and I feel is a mishmash of old and new) and googles absiel which I haven't actually tried enough to make an opinion about.
Re:Modern C++ Won't Save Us by geezerjay on Tuesday April 23, 2019 @01:48AM (#19725907)
> Can you or others post such alternative standard libraries?

POCO comes to mind.

https://pocoproject.org/

Re:Modern C++ Won't Save Us by jcelerier on Tuesday April 23, 2019 @03:29AM (#19726345)
Also Qt is basically a Java-like library, with bulit-in stuff for networking, GUI, XML, JSON, WebSockets, multimedia, etc.

Or you have some "domain-specific" libraries like OpenFrameworks which is very nice if you are making visual art since it comes with a lot of very simple primitives to draw shapes, etc.

Re:Modern C++ Won't Save Us by humanrebar on Tuesday April 23, 2019 @07:24AM (#19727452)
ACE was an attempt that is basically dead.

There was some talk about an std2, but I gather support for it is too low to be pursued seriously.

Re:Modern C++ Won't Save Us by lallysingh on Monday April 22, 2019 @11:17PM (#19725295)
What legacy? It's not like there was a single "before time." There are problems coming up with all of it, because the underlying runtime model provides too few guarantees. We'll be plugging holes the rest of our natural lives.
Re:Modern C++ Won't Save Us by millstone on Monday April 22, 2019 @11:53PM (#19725482)
No, the problem is NOT the underlying runtime model. In fact it's often the opposite: the STL tries to provide too much.

An excellent example is std::unordered_map. This type was introduced to address perf problems with std::map. But unordered_map forces closed addressing, separate allocation, etc. which limit its performance. In return you get stronger iterator invalidation guarantees but these are rarely useful. Meanwhile Abseil's swiss tables, LLVM's DenseMap, etc. illustrate what a high-performance C++ hash table could be.