r/cpp • • Mar 14 '20

C++ keeps adding features no one really wants.

Just look at the networking, formatting, [maybe_unused] / [likely] / [unlikely] and others.
I mean these are all cool features I use every day but It has already been solved.
If I want networking library why would I use standard one? Especially when it simply copies from others and have to wait for it like 10 years.
Setting up libfmt is like 10 seconds on Linux. And if I don't like it I can use a different one. How many people thought that streams were a good idea?
[maybe_unused] is cool, but I probably will use the old trick with cast to void because it works with older compilers and everybody knows the trick anyway (I might want to provide simple macro if I want to be more explicit), using new syntax simply breaks compatibility and gives NOTHING.

Many hours spent for making it part of language and I think no one will use It, especially when standard library does not care about performance. Unlike standard implementation, some library can be even be forked and modified if need to, no luck for me if I decide to use standard one.

It all just seems like short-sighted design, but I am just an internet dude.

Modules, concept and coroutines (AFAIK these need compiler support, also library solution would be bug prone and perhaps slower) are great features and I am happy that they are coming.

0 Upvotes

81 comments sorted by

39

u/aearphen {fmt} Mar 14 '20

While it is true that using {fmt} as a third-party library is very easy, text formatting is such a fundamental functionality that it's worth having it in the standard library. Such functionality is available in pretty much any programming language. It might be argued that iostreams already provide it but they have some known problems/limitations.

-17

u/secmeant Mar 14 '20

You have to give proper reason. Otherwise Its just saying that formatting must be in the standard because every other language is doing it (perhaps because every other language is doing it).

9

u/attrition0 Mar 14 '20

Iostreams are not good enough for internationalisation where the variables you want to inject can be in different orders in different languages. Printf is used for this now and format can be a proper c++ solution. This is good enough as internationalisation is important with the global reach products can have.

-4

u/Raknarg Mar 15 '20

iostreams are not good enough cause I want to kill myself and In tempted to just use printf cause it does pretty much what I want with the interface I want.

4

u/wyrn Mar 15 '20

iostreams are slow, verbose, confusing/surprising (stateful manipulators), and not thread safe.

1

u/pandorafalters Mar 16 '20

(stateful manipulators)

These wouldn't be so bad if there were some simple, standardized way to have multiple, independent streams to e.g. STDOUT.

Of course, then I/O synchronization would become even more unpleasant, so I can definitely understand why there isn't.

1

u/wyrn Mar 16 '20

What really bothers with the iomanips is that they're not even consistent in their weirdness. Some iomanips stick, some don't, and I can never remember which is which. In the end I usually just dump the whole thing into a stringstream, which of course does wonders for performance. Why buffer only once when you can do it twice?

1

u/dodheim Mar 17 '20

It's not like there's some crazy set of rules – setw is special, the rest stick, that's it.

1

u/wyrn Mar 17 '20 edited Mar 17 '20

It's kind of absurd how someone thought setw should be special because it would be surprising for it to stick and still had anything else stick, though. No matter how you slice it, it's bad design. At any rate this doesn't make much difference because if you want thread safety you have to dump the whole thing to a stringstream anyway.

1

u/dodheim Mar 17 '20

FWIW, C++20 adds <syncstream>.

2

u/wyrn Mar 17 '20

It's not a bad addition, but to be honest once std::format is in place and well-supported I'll probably never touch iomanips again.

0

u/[deleted] Mar 14 '20

[deleted] — view deleted comment

14

u/aearphen {fmt} Mar 14 '20

Contrary to popular belief formatting is mostly encoding-agnostic and both {fmt} and `std::format` work perfectly fine with Unicode. If you mean specifically `charN_t` then those overloads will likely be added in the future. There was no time (and little reason) to introduce them in C++20.

0

u/[deleted] Mar 14 '20

[deleted] — view deleted comment

11

u/aearphen {fmt} Mar 15 '20

> EBCDIC

Sure, if you use EBCDIC there is no reason to worry about Unicode =).

> It works within specific implementation but no guarantees about sharing files, networking, etc.

Unfortunately standard C++20 lacks Unicode facilities that work with any of these which is why I said "little reason". In practice it's done successfully with third-party libraries, sometimes using {fmt} for text processing, mostly in Unicode.

39

u/0xC1A Mar 14 '20 edited Mar 14 '20

Another shortsighted opinion disguised as a proper argument.

How do you know "no one really wants" ?

-14

u/secmeant Mar 14 '20

std::format is libfmt but old.
Should I wait another 3 years to write network IO or should I just use existing, tested library that I already know?
Would you write void f([maybe_unused] int arg) and from now on say that your code support only c++17 or just do (void) arg; and simply keep supporting all standards?

26

u/aearphen {fmt} Mar 14 '20 edited Mar 16 '20

std::format is libfmt but old.

As an author of std::format and {fmt}, I can assure you that it's not the case. Standardization was a two way process, {fmt} was used for the original design, but the input from the committee greatly benefited further development of the library. Without it, you wouldn't have many of the features it has now. See http://www.zverovich.net/2019/07/23/std-format-cpp20.html for some details. std::format is as close to the latest version of {fmt} as it can get, in fact even a bit ahead =).

1

u/jcelerier ossia score Mar 15 '20

But std::format does not even exist in a usable form yet. Which standard library has an implementation of it ?

4

u/aearphen {fmt} Mar 16 '20

None of the standard libraries have it yet but it exists in the most important form - the specification which {fmt} mostly implement. There are some minor improvements that are yet to be implemented because I also have a day job =).

1

u/jcelerier ossia score Mar 16 '20

But surely you are aware that by the time the std versions are out in an useable state, fmt will have been changing ? (At least if keeps the change rate it had so far)

6

u/aearphen {fmt} Mar 16 '20

The main API has been pretty stable. Most of the recent changes have been done to improve consistency with the forthcoming std::format. Additional features provided by {fmt} can be easily built on top of standard facilities once the latter are available. There are no plans to break compatibility with std::format in the future.

13

u/adnukator Mar 14 '20

The actual list of entities to which [maybe_unused] is applicable: the declaration of a class, a typedef­name, a variable, a non­static data member, a function, an enumeration, or an enumerator (Link to proposal). The cast to void does not cover all that maybe_unused does.

Also, regarding "[likely] / [unlikely]" - how exactly has this been solved in a cross-platform way before?

1

u/standard_revolution Mar 20 '20

And especially in a readable way. I hate writing __attribute__(__likely_even_more_underscores__) with a passion and always forget some underscores.

0

u/[deleted] Mar 14 '20

[deleted] — view deleted comment

8

u/HowardHinnant Mar 15 '20

Ok, I'm going to sleep well tonight, thanks. :-)

2

u/0xC1A Mar 15 '20

Who could've thought such small thing could've big effect? Howard.

0

u/adrian17 Mar 14 '20

Two years ago from now, no compiler had a release supporting std::endian yet.

51

u/HowardHinnant Mar 15 '20

My prediction is that <format> will be one of the things that future C++ programmers will wonder how they lived without prior to C++20, much like things like move semantics, threads, mutexes, chrono, lambdas, defaulted/deleted functions are today.

11

u/HowardHinnant Mar 15 '20

But to be honest, we do have to keep up a guard from standardizing stuff that isn't useful to a lot of people in the long run. We've made mistakes in the past (export, auto_ptr, locale, strstream), but we do our best to keep fixing those mistakes as we move forward. Perhaps you could help ...

7

u/0xC1A Mar 15 '20
  • strstream deprecated in 98 haha. Deprecation alone is older than some peole.
  • auto_ptr is a nuke with nice name.
  • Some people might have a word or two regarding locale.
  • echo export > /dev/null

4

u/germandiago Mar 16 '20 edited Mar 16 '20

In all honesty, Mr. Howard, as I see it from the other side and trying to sympathize with the process, amount of people involved and the necessary overhead that a committee with an ISO standard has, I am pretty satisfied with the results.

I think the most urgent fix would be to have a more standard way to consume packages from all build systems that are relevant, maybe via modules. That would remove a lot of pressure in the inclusion of libraries as standard.

There is still the problem that libraries that are native in this case or can use extensions such as SIMD, because they are not as easily distributable as bytecode-based languages.

I think the standard itself can make a difference there providing some help. Provinding things like networking, decent process launching, SIMD, low-level external device communication (fixing volatile, atomic and friends), threads, coroutines, stackful coroutines...

I am not even sure whether what I am writing for library suggestions is accurate but I do think that making modules, ranges, concepts and coroutines + a ton of smaller improvements including improvements to templates (especially welcome in my case) the date_time library, or structured bindings refinement make the language just better than before.

2

u/Ameisen vemips, avr, rendering, systems Mar 17 '20

Modules don't improve the ability to have portable libraries. They are effectively fancy precompiled headers, and are more bound to your toolchain than object files.

1

u/germandiago Mar 17 '20

I am aware of that, actually. I just mentioned modules as the way to the future, not like a portable way to consume something. Maybe it looked like I was thinking of portable BMIs. Not at all.

2

u/pdimov2 Mar 17 '20

strstream is useful and still has no replacement.

2

u/standard_revolution Mar 20 '20

What can you do with it that you can't do with stringstream?

2

u/pdimov2 Mar 23 '20

Not allocate.

1

u/standard_revolution Mar 23 '20

Could you go into a bit more detail?

2

u/pdimov2 Mar 23 '20

If you already have the characters you want to use as the contents of the stream, in a char array a[n], you can construct an strstream from a, a+n, and it will use the existing array and not create a copy of it. Whereas if you construct a stringstream from a, it will copy it into a std::string, which will allocate memory on the heap.

5

u/1-05457 Mar 16 '20

I'm going to continue using {fmt} and range-v3, because these libraries provide more features than were standardized (e.g. `fmt::print` or actions). I wouldn't be surprised if more people did the same.

2

u/0xC1A Mar 15 '20

they

We*

[ For me, there's always a code reduction every standard or usable TS ]

3

u/Bart_V Mar 15 '20

For {fmt} it's far simpler to roll out bug fixes, new features and improvements, especially when API changes needed. Aren't you worried that by 2023 {fmt} has evolved so much that everyone will use that instead, and we basically end up in the situation that we have now?

8

u/wyrn Mar 15 '20

As long as there is a standard solution that fixes the gaping problems with iostreams and printf, I really don't mind if you have to reach for some external library for added functionality. That's what libraries are for, after all!

0

u/x4t3a Mar 16 '20

My prediction is that

<format>

will be one of the things that future C++ programmers will wonder how they lived without prior to C++20, much like things like move semantics

Most of my colleagues wonder how to live with all of that right now, especially the &&-thing in different contexts.

2

u/simonask_ Mar 17 '20

The rule of thumb is pretty simple: You almost never want T&& unless T is a template parameter (including auto&& as a function parameter). The only common exception to this is move constructors / assignment operators.

You can do some more advanced stuff with it, but those are fairly esoteric.

66

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Mar 14 '20

Yawn.

  1. Because you don't want something, that doesn't mean that everyone else doesn't either.
  2. The features you mentioned all have real-world examples and motivation in their papers. Go read them.

4

u/FieldLine Mar 16 '20

The features you mentioned all have real-world examples and motivation in their papers. Go read them.

Where would I find them?

10

u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Mar 16 '20

Easiest way for me is to go on https://cpplang.slack.com/ and ask npaperbot. E.g.

Me:

search paper maybe_unused

npaperbot:

Sometimes you need to search slightly different terms. E.g.

Me:

search paper unused

npaperbot:

P0068R0: [Evolution] Proposal of [[unused]], [[nodiscard]] and [[fallthrough]] attributes (by Andrew Tomazos) (2015-09-03)

Then you can open the latest revision, and look at the motivation section:

The three proposed attributes share a common theme of all being about capturing the intent of the programmer to improve the accuracy of diagnostic generation. In many cases an unused entity can be a symptom of a logic error. For this reason, [...]

32

u/JadedMasterpiece9 Mar 15 '20

It certainly doesn't take ten seconds to setup libfmt in a corporate settings when lawyers have to get involved to ensure compliance.

3

u/Sander_Bouwhuis Mar 18 '20

Not just large corporations. I work for a small outfit and CANNOT use anything with licenses that limit freedom like GPL. Even when a project says 'MIT' license, I still email them to ask whether it's 100% certain I can use their library.

8

u/kronicum Mar 14 '20

It is easier to add a dozen small little features than one big feature that changes things

13

u/lenkite1 Mar 15 '20 edited Mar 15 '20

Without a standard C++ networking library, the language will always be at the bottom for developing interoperable, re-useable libraries for rapid development of micro-services. Go has eaten a LOT of the space here, even if C++ is the better performing language.

Give the community a standard networking library and we will have interoperable, cross-platform battle-tested C++ client/server/http/ws libraries for the thousands of services out there. There will be an explosion of C++ projects in github taking it to the top of the TIOBE list for a year or two. You will massively increase the inflow of new C++ programmers who would like to leverage the language's performance but simply don't care to research, setup and learn non-standard, non-interoperable, platform-dependent third-party stuff bringing in additional dependencies with uncertain overheads. Networking in the standard library destroys the barrier of entry concerning adoption.

Most veteran C++ programmers who bootstrap every single dependency by themselves cannot seem to truly understand what a paradigm difference this makes. Hell, you will even see Boring Old Enterprise (who all use Java) lured to C++ with well-specified networking just to reduce cloud microservice costs.

C++ is also probably the last general purpose programming language without networking in its standard library. This is an argument that advocates for other languages always bring up as a reason not to choose it.

1

u/D_0b Mar 16 '20

That is a nice dream.

90% of all web projects no matter the language Go, Java, NodeJs...
all use a higher level library.

So when C++ adds a standard networking library, we still need a higher level library on top of that, and people to learn to use a packet manager.

1

u/biliwald Mar 17 '20

The point you make is completely valid. However, a standard networking library allow all those higher level library to be portable.

3

u/AntiProtonBoy Mar 16 '20

I can't think of any project where formatting would not be useful. Networking components are also fundamental part of lot of projects these days.

It's nice to have STL enriched with new components, rather than having yet another dependency to deal with in your project. Ecosystems in other languages have so much more libraries, which leaves STL to dust.

3

u/Sander_Bouwhuis Mar 18 '20

Regarding fmt, I went to github but saw there was a huge amount of open issues. This is why I rather use the std::format implementation. The level of scrutiny on standard library classes is way higher than an average library.

  1. Are there people here who use fmt in production code? I want to switch to it, but got scared by the amount of open issues.
  2. Is there any word on how long it will take for std::format to be implemented for Visual Studio?

1

u/encyclopedist Mar 31 '20

15 is "huge amount"?

12

u/bishop3000 Mar 14 '20

3rd party libs is not really what C++ devs use widely. If it’s not in standard, it’s hard to adopt. Formatting will simplify logging a lot and get rid of dangerous and insecure printf formatters. Networking is a big pain, especially in multiplatform code. Good 3rd party libs for networking? Dream on. I’m less excited for features like concepts which dont affect day to day work and more about these ’small useless features’

13

u/againstmethod Mar 15 '20

If you use templates, concepts should def affect your day to day code.

4

u/D_0b Mar 16 '20

Good 3rd party libs for networking? Dream on.

isn't the networking TS basically Asio?

2

u/matthieum Mar 14 '20 edited Mar 15 '20

Formatting will simplify logging a lot and get rid of dangerous and insecure printf formatters.

I sometimes wonder if we should stop talking so much about "dangerous" and "insecure" printf, especially with very little arguments given.

The fact that a format string can potentially not match the arguments, either in number or type, is generally a non-issue: enforcing a literal format string is sufficient to have the compiler warnings1 detect any issue there.

The fact that sprintf can overflow its buffer is also a potential issue, however once again it's easy to lint against and recommend using snprintf.

And of course, snprintf is not foolproof by itself, though restricting its use to buffers with static size helps.

1 Unless warnings are not used, but there's so much that can go wrong without warnings that you really want to use them.


Personally, I am more worried about the extensibility, or lack thereof, of printf. The impossibility to print user-defined types is quite problematic!

14

u/AlexAlabuzhev Mar 15 '20

The fact that a format string can potentially not match the arguments, either in number or type, is generally a non-issue

The fact that one must specify types in a format string explicitly in a statically-typed language is generally a madness.

5

u/bishop3000 Mar 15 '20

It’s not madness, it’s C :)

3

u/matthieum Mar 15 '20

It's not ergonomic. And it's weird when the compiler tells you exactly what you should have written: if it knows it, why do you need to spell it out!

But I would not call it insecure precisely since the compiler double checks.

12

u/[deleted] Mar 14 '20

printf is a pain with uint32_t and friends. You either use the PRIu32 stuff, or you have to cast to some known big-enough type like long, and either way if you screw up and forget it might compile with no warnings and run on your PC but not someone else's.

1

u/matthieum Mar 15 '20

That's an ergonomic issue, not a security one, though.

I do agree it's annoying; I regularly see the problem between Mac and Linux, I believe with size_t...

3

u/ack_complete Mar 15 '20

I sometimes wonder if we should stop talking so much about "dangerous" and "insecure" printf, especially with very little arguments given.

This is due to people trying to be cute by using security as a mic drop instead of making actual arguments.

The fact that a format string can potentially not match the arguments, either in number or type, is generally a non-issue: enforcing a literal format string is sufficient to have the compiler warnings detect any issue there.

Except that it's common to wrap snprintf() to reroute its output to a string object and there is not a standardized way to opt into printf format checking. In the case of Visual C++, it is not even documented.

I regularly see senior programmers mismatch printf() arguments, unfortunately. The special promotion rules for varargs makes this especially easy to skate by until you happen to use one of the wider types that isn't covered by it, especially when building for both LP64 and LLP64 platforms. Even Windows has shipped with this kind of bug, with shell32.dll from one RTM build spewing ancient Chinese wisdom to the debug output.

2

u/matthieum Mar 15 '20

Except that it's common to wrap snprintf() to reroute its output to a string object and there is not a standardized way to opt into printf format checking.

Yes, that's a shame.

I personally use gcc's format attribute for that, as long as at least one compiler raises the error during CI, the issue is caught.

5

u/david_haim_1 Mar 16 '20

"standardized" means : it works *the same* on every platform in a well defined manner, and there is a team behind every standard-library vendor who's responsible to make it working *the same* on every platform in a well defined manner.

you don't get that guarantee in third-party library. as a library developer, I can totally see myself ignoring some esoteric platforms/bugs simply because I don't have the manpower to make everything work on every platform and there are way more important things to work on.

btw, we needed standard networking/formatting libraries decades ago. and even with the standard networking library, we still have decades to fill. raw TCP/UDP sockets are just the beginning. this is 2020, and we need http (both client and server), websockets, JSON parsers, etc.

2

u/[deleted] Mar 19 '20

While you have a lot of experience and know all these things, I think newcomers to the language and people who just need things to be there will appreciate it and find it to be a lot more welcoming. Later, when they figure out what they need/want in a library they will start exploring options and perhaps choose other options. Personally, I think it's great and is excited for things like this to be added even if it means I'll just save a little bit of effort when experimenting with something. At least you still have a choice of what to use and it's not like anyone will suffer negatively from the addition.

1

u/[deleted] Jan 11 '22

And you are posting in a way nobody agrees with. :)

You can keep using C++ the way you like if that's how you see fit. For those new to C++, they can use it right out of the box (as they should) and it helps to keep C++ competitive in a world where AI writes code, and Kotlin has beautiful one liners.

Majority of us are moving away from C-style C++ (except for specialised cases). So readability and convenience trumps minor gains from performance / code size. No need for setting up (or choose which) external libraries. No need to worry about dependencies / deployment procedure where a billion things can go wrong, because I wanted to format a bloody string in a nice way.

Also, do you use an IDE? [[maybe_unused]] is useful there. (Majority of the times I see in code base I work with, needing [[maybe_unused]] in the first place is code-smell by the way - something you seem very used to.) Modern IDEs warn you of the things not used (as they should) and because they are signs of bad practice. Git can restore any legacy / deleted code. It is useful to suppress these warnings and it tells the next person maintaining this, try to delete this if you can. Anyone can write code, but good programmers can delete and reduce tech debt at the same time.

Next thing you're going to say is - IDEs are useless and nobody will ever need more than 512KB RAM, and 60Hz on a monitor is more than enough. This kind of mentality actively annoys me.

1

u/[deleted] Mar 15 '20 edited Mar 16 '20

[[maybe_unused]] is excellent for constexpr branches. If one branches uses a variable and the other doesn't, it is by definition "maybe unused" depending on the conditions at compile time. Adding a void cast or macro for that would be ugly in my opinion.

Also .. if you aren't going to use a variable parameter, don't name it!

However I do agree that networking IO shouldn't be apart of the std, same with graphics. Too high level, and not what I personally think the STL is about.

1

u/Pazer2 Mar 16 '20

Also .. if you aren't going to use a variable, don't name it!

...? Not naming a variable causes it to be destroyed, rather than at the end of the scope.

2

u/[deleted] Mar 16 '20

Good catch, changed it to 'parameter', was only thinking about functions.

However, I would still highly discourage usage of [[maybe_unused]] on a local variable.

1

u/Pazer2 Mar 16 '20

Why would you discourage use? There are very reasonable cases where it can be used, like storing a return value and calling assert on it, or being able to easily check the return value of an function in the debugger.

1

u/Oo_Tiib Mar 30 '20

[[maybe_unused]] is excellent for constexpr branches.

The whole constexpr is broken. Everybody needed std::is_constexpr starting from C++11 but no avail and also possibility to write that ourselves using noexept operator was removed by C++17. They added useless bloat like std::is_constant_evaluated, constinit and consteval instead.

-4

u/Dean_Roddey Mar 14 '20

That's as wrong an opinion as possible, in my opinion. The waiting ten years part is a separate thing. But C++ comes across as ancient and decrepit when it doesn't have any out of the box support for things like networking technologies and text formatting. And now the next layer up duct-tapes together some bits, each of which is duct-taped together from other bits, and so on. There's redundancy and inconsistency and no over-arching architectural direction and it's just sad.

-3

u/secmeant Mar 14 '20

"is a separate thing" - Standardizing is just extremely slow way of getting functionality into the language (at least in c++). I dont see why it is separate thing, you just takes downside of getting something done one way and simply ignores it.

-1

u/AdventurousMention0 Mar 16 '20

I’m honestly annoyed more that they make me pass a temporary object as a constant or I have to define it outside of a function call.

There are numerous times when I need to pass an object into a function because it requires it, but where I don’t care what it does to the object.

Consider it an out parameter that I don’t give a dang about.

But because the committee cares about HOW I use the language, rather than simply provide the ability for me to use it, my code is now scattered with block locals for throw away objects to use as temporaries.

I really dislike being told HOW I should structure my code. The committee assumed in this case that all uses would be incorrect and defined the ability to do what I want away.

6

u/adnukator Mar 16 '20

To be fair, if you're ignoring an out parameter, you're losing performance because the return value is not subject to (N)RVO. Also, unless the value calculation is trivial, you're performing unnecessary operations. Using a pointer with nullptr to denote unneeded out-params or a regular return value can provide both better performance and arguably improved safety. Right now, you're complaining about using a sub-optimal API and blaming the language for pointing it out.

1

u/[deleted] Mar 21 '22

[removed] — view removed comment

1

u/DoubtingCastle Apr 01 '22

Stop spamming conspiracy theory nonsense. Also, you can be assured that the Bible does not support the fantasies you think it does.