r/cpp • • Apr 03 '18

Newer C++ features can create a lot of system yak shaving

https://rachelbythebay.com/w/2018/04/02/cpp/
2 Upvotes

28 comments sorted by

19

u/[deleted] Apr 03 '18 edited Apr 03 '18

One of these was a specific flavor of smart pointer called scoped_ptr which worked just like the one at a certain company. It gives you a few neat things, like the ability to just forward-declare a class inside your .h file and not include the whole thing right there. This makes your dependencies simpler and compiling tend to be faster, too.

What does a forward declaration have to do with a scoped_ptr? Am I reading it wrong?

This whole article is less about C++ and more about a particular version of a linux distro that packaged a specific version of a C++ compiler years ago. I'm not sure what the takeaway from this article is supposed to be.

edit: grammar

3

u/Sanae_ Apr 03 '18

What does a forward declaration have to do with a scoped_ptr? Am I reading it wrong?

I guess they wanted to use the PIMPL idiom + using smart pointers; thus a scoped_ptr (should be close to a unique_ptr). Some extra reasons I think of:

  • not having to deletethe impl pointer.

  • const-correctness

  • combining PIMPL and value semantics if need be if a clone-like method exist.


Takeaways is, while C++ is moving again is producing benefits, it's also introducing headaches because

  • the ABI isn't stable

  • and not everyone is advancing at the same pace, or sometimes something is unable to advance.

5

u/[deleted] Apr 03 '18

I'm comfortable with people using a decade old distribution not being comfortable though.

Honestly, I wish we were more comfortable with breaking ABI and even API compatibility. It's better in the long run even if it causes short term discomfort.

As to your first point, ?. I know the class she is talking about, I'm saying she mistakenly wrote that it grants the ability to forward declare the class.

3

u/Sanae_ Apr 03 '18

I'm comfortable with people using a decade old distribution not being comfortable though.

If it's only decade-old stuff that is breaking, yeah, that would be ok. Not great, just ok, becasue C++ used on long-lived programs.

But program written with few-year old compilers have also issues. In the end, the safest is to compile everything with the same cpompiler, but it's not always possible (properitary lib; yeah, FOSS is great, but sometimes you have to work with non-FOSS stuff) or hard (build system fragmentation).

even API compatibility

This would made the problem worse, because people will then have to stop updating their compiler. Again, many C++ have long lives. And need to have some form of certifications, which can be complex/long/expensive.

I sometimes wish the same for ergonomic/readability/etc reasons, but I know it's not doable project-wise.


I haven't used scoped_ptr, but I thought you could pre-declare the class, like so:

class Engine;

class Car
{
    scoped_ptr<Engine> engine_;
    // yadda yadda...
};

instead of :

#include "Engine.h" // can't pre-declare

class Car
{
    Engine engine_;
    // yadda yadda...
};

I believe there is some rule about std::unique_ptrand whether the class has a special deleter or not. Can't look it up right now.

3

u/[deleted] Apr 03 '18

Just use Engine* engine and forward declare it. Or use it as a template argument in some other ref or pointer wrapping class.

I've always been a fan of forcing people to update. If they would rather stop updating the compiler and let their codebase rot so be it. Periodic maintenance on a 3 year tick is really not too much to ask in my opinion. And C++ will always support the same breadth of platforms, so even if your systems can't update, your build infrastructure can, independently of the runtime platform.

3

u/Sanae_ Apr 03 '18

Just use Engine* engine and forward declare it.

  • No automatic releases of resources (even if it's trivial in the usual PIMPL idiom).

  • Wacky const-correcteness, that has to be solved by being careful/macros.

I've always been a fan of forcing people to update. If they would rather stop updating the compiler and let their codebase rot so be it.

When those people are your clients, and when they have payed for 2, 5 or 10 years of maintenance, updates, especially when they break stuff, can become a big no-no.

Periodic maintenance on a 3 year tick is really not too much to ask in my opinion.

Look, this may be true for medium systems or stuff you can easily test and such.

It's not true at all for larger projects or megaprojects, or complex ones (ones with billion LOC, with 3rd party dependencies that can go go in the dozens or hundredths.)

And C++ will always support the same breadth of platforms

Even if true: it doesn't matter; if it break a critical library that can't be ported (because team is too busy, because it's third-party and you don't have the source, because the source is too complex, because upper-management doesn't want to pay for the update, ...) and can't be swapped (because too complex, etc.), then you're screwed.

0

u/[deleted] Apr 03 '18

Gosh you really aren't reading the original post. Nobody is talking about the advantages of an RAII pointer. Just that it isn't required to forward declare something (which is what she wrote originally). Full stop. If you're still lost after this, I don't really know what to tell you haha.

As for your second point nobody is forcing you to update your compiler! Just keep shipping C++03 code in 2100 for all I care. I've worked in those megacodebases I'm aware. But the reason everything ossifies so much is precisely because the culture has bred mentalities like your own.

4

u/Sanae_ Apr 03 '18 edited Apr 04 '18

Of course a smart pointer isn't required to have forward declaration.

My point is author wrote a smart pointer to avoid a forward declaration, because the considered choice was "smart pointer + forward declaration" vs "simple value attribute (non-pointer) + full include", and not "smart pointer" vs "raw pointer" - as author wants RAII. Ofc it's an educated guess, deduced from the context.

This is why I reacted to:

What does a forward declaration have to do with a scoped_ptr? Am I reading it wrong?

Because yeah, you were reading wrong.

As for your second point nobody is forcing you to update your compiler!

Sort of OP's point basically. Having to make the choice between going stale and breaking everything is a terrible choice to make.

But the reason everything ossifies so much is precisely because the culture has bred mentalities like your own.

Yeah, personal attacks, nice. FYI, I prefer to update as much as possible. I'm not the one who negotiated those contracts, who decided not to wrote test and such. I'm the one who arrive with a heavy codebase, and learn and try to do things are are possible, while pushing to improve stuff.

"Go fast and break things" is just not possible in some industries, some of which are huge C++ users. get that in your head and some bother ring other people.

1

u/hyperactiveinstinct Apr 04 '18

This subreddit is a joke... you’re being downvoted for being reasonable, trying to show the opposite viewpoint without really spousing it as your own, and receiving personal attacks in the process.

6

u/NotInUse Apr 03 '18

The takeaway is a lot of businesses don’t run bleeding edge systems (not at all limited to linux) and that makes using newer features far more difficult. The scale of dependencies on third party libraries makes running multiple sets of compilers and libraries prohibitive.

17

u/kalmoc Apr 03 '18 edited Apr 03 '18

There is a difference between not using bleeding edge and using a compiler that is probably more than half a decade old.

If you want (for whatever reason) to use a completely outdated platform, then there is some cost associated to it and you have to decide wether or not it outweighs the other benefits of using that platform. That is the root cause of your problem - not the evolution of c++. The particular "problem" with c++ on Linux (on Windows you have other problems) is the tight integration of the build tools with the distribution.

Btw.: The developer tools try to address precisely that problem: https://developers.redhat.com/products/developertoolset/overview/

8

u/sumo952 Apr 03 '18

I couldn't upvote this enough. +1000 :-)))

There's also a security risk of using such ancient compilers.

And it's actually much more than half a decade (5 years wouldn't even be so bad, lol!): RHEL6 has gcc-4.4 as default compiler, which was released in 2009. Ok - it had its last patch-release in 2012... so 6 years, if you count in RHEL's favor. But we're sort-of approaching talking about a decade-year old compiler here. In the fast-evolving world of software & IT... Just imagine having to ship something with 2012's HTML & JavaScript, today.

Btw, RHEL7 ships with gcc-4.8 from from 2013 - not too much better. In 2018.

4

u/raevnos Apr 03 '18

OpenBSD 6.3, which was just released, comes with gcc 4.2.

(And clang 5, but I haven't checked yet to see if that uses libc++, or if it shares a decade old libstdc++ with gcc)

1

u/kalmoc Apr 04 '18

Yeah, personally, I consider gcc-4.8 the oldest compiler worth supporting. It has decent (although not full) c++11 support and is the default compiler on ubuntu14.04 & rhel7. Debian Wheezy has even older gcc 4.7 but support is about to end anyway and afaik Jessie comes with 4.9.

5

u/[deleted] Apr 03 '18

It's really about C++'s lack of willingness to enforce a stable ABI.

6

u/smdowney WG21, Text/Unicode SG, optional<T&> Apr 03 '18

The language has had a remarkably stable ABI. 17 is introducing one change, making noexcept part of the signature, but that's been it for a long while. The std library changed std::string such that it isn't copy-on-write, and most now use a small string optimization.

However, other libraries are more likely to make changes. The header may make use of new features of the language, and will therefore be incompatible with other, old, compilations. C++ binds early and eagerly, making it highly optimizable, at the cost of ODR problems. Undefined behavior, no diagnostic required.

2

u/[deleted] Apr 03 '18

I've had builds fail when trying to link a g++ compiled lib with a clang compiled exe. I've had crashes due to distro-supplied boost shared libraries not being binary compatible with my application's compile-time options.

That's not stability IMO.

https://stackoverflow.com/questions/35225234/linking-libboost-log-so-makes-boostasioio-servicerun-exit-immediately/35241050#35241050

1

u/smdowney WG21, Text/Unicode SG, optional<T&> Apr 04 '18

The first should be a bug, if you compiled against the same standard library. You can't mix two definitions of std::vector, for example.

And, yeah, if you want to link system libraries, you have to make sure you're using the same ABI affecting flags, which usually means the same std level, -D options, etc.

Other languages deal with this differently. Usually by compiling much later, if at all. C has the same problem, although somewhat less because there's less inlining.

3

u/oldprogrammer Apr 03 '18

Been a while since I did any heavy C++ coding, but I was thinking about the library issues he brings up the other day.

Aren't the linkers smart enough to only pull in the pieces of the library that are used in the binary or does it end up binding the entire library into the binary?

If it is the former then what is the main reason for not going static? Sizes shouldn't get too bad, you eliminate the dynamic loading requirements and don't run the risk of wrong libraries being loaded.

If it is the later then what about an idea of the library being either pre-compiled .o files or just delivered as the source only with defined dependencies, then projects just pull in what they need?

0

u/[deleted] Apr 03 '18

[deleted]

1

u/oldprogrammer Apr 03 '18

Imagine that you have X programs that statically link against library Y. If there is a bug in Y you need to recompile and distribute and install X programs. If they are dynamically linked you can simply replace Y.

While a true scenario, if a new version of Y is deployed that has a bug or a compatibility issue then you've broken all X applications as well.

The problem is that c++ doesn't have an single dependency management/ build tool

This is I think the biggest issue, but what would such a dependency management/build tool look like for C++ since the end result is native binaries? Would it fetch say some form of pre-compiled or source code and build it on the fly for the target platform?

Or do we just need a Maven type repository with pre-compiled binaries for each platform and use a classifier like we do in Java for native jars as for example the lwjgl (opengl for java) does to fetch the platform shared libs?

2

u/sumo952 Apr 03 '18

There was this thing linked a week or so ago here which makes newer compilers available on RHEL I think.

6

u/TManhente Apr 03 '18

Do you mean the Red Hat Developer Toolset?

1

u/sumo952 Apr 03 '18

Yea I think that was it!

1

u/saimen54 Apr 03 '18

Using it since last week and so far no issues, but shiny GCC 7.2

1

u/r2vcap Apr 03 '18

is there are any problems with -Wl,-rpath=$ORIGIN/../lib ? No, I don't think so. Just shipping libstdc++ and libgcc_s if you need to use newer C++ compiler and can't use RHEL's devtoolset.

2

u/[deleted] Apr 03 '18

It's good for application deployment. It's not enough if you're writing a library and end user will load his preferred libstd first.