Need some advice from C++ programming experts

If it's not ZDoom, it goes here.
Teddipetzi
Posts: 42
Joined: Sat Aug 17, 2019 2:32 am
Operating System: Windows 10/8.1/8/201x 64-bit
Graphics Processor: nVidia with Vulkan support

Need some advice from C++ programming experts

Post by Teddipetzi »

I've been taking some C++ programming lessons recently, and come up with a few questions that cannot seem to be answered by the gurus in any satisfying fashion, this mainly concerns input/output options in C++.
The thing that confuse me is the C++ iostreams. Nearly everybody talking about this subject says they are better because they are type safe and their demo code makes rather liberal use of them.
But when I take a look at some real world code bases, standard C stdio often seems to be preferred by seasoned programmers, both for text output and for file I/O. GZDoom is a good example for this - out side of third party code it completely steers clear of C++'s I/O code.
Is there a reason why they are often shunned?
dpJudas
 
 
Posts: 2860
Joined: Sat May 28, 2016 1:01 pm

Re: Need some advice from C++ programming experts

Post by dpJudas »

There are a couple of reasons:

1) Character sets, unicode and code pages. For some reason the C++ standard committee could never agree on making unicode a requirement for those implementing C++, so if you use the C++ iostreams it becomes platform specific what character set is actually being used. On modern Linux it is UTF-8, while on Windows it is either the local ANSI or OEM code page.

2) Like std::string, the iostream's API is generally crap. For example if you want to output in hex you first have to << std::hex and then after << std::dec to reset the state. At the same time it can't output std::string without needing .c_str() on them. Generally speaking, developers tend not to use the C++ library whenever the design drops below a certain quality level. At that point they usually switch to alternatives as that's usually a one-time cost per project.

3) Due to #1 you're already platform specific, so might as well just use the C language or platform specific alternatives.

4) A lot of C++ codebases are very old. Some span decades. The C++ standard in 2020 is a lot better than it was 20 years ago. There were even more reasons to shun the iostreams in the past.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 47986
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Need some advice from C++ programming experts

Post by Graf Zahl »

Seconding the "because it's crap" sentiment. For outputting text it's an overcomplicated clusterfuck but things do not stop there.

Ultimately both stdio and iostreams have the same underlying problem, which is that they treat terminal output as an inherent property of a file instead of a specialization.
This means that all the special handling for terminals is coded right into the core of the whole thing. In C with a small function interface this rarely becomes an issue outside of code bloat in the library, but in C++ it infests the entire file stream hierarchy from top to bottom with mostly awful design patterns that the user needs to be aware of. A result of this screwed up design is that it is virtually impossible to specialize the streams for virtual file systems or other non-traditional cases because you can't just inherit from them and expect it to work. Virtually everything here has been done wrong by putting the file at the bottom and all the terminal baggage right with it. This means you can't just derive a special class from it because it got no working interface for that. A proper design would have put the abstract interface at the bottom and then specialize it for use with files. It's one of those monstrosities that only get carried forward by their own inertia, not because they are any good. If you ask me, they are one of the worst parts of the entire C++ standard library.

There's a good reason why for GZDoom I rolled out my own FileReader interface that can do what it needs: Transparently handle files from different sources like resource archives or memory buffers, or if later needed, data caches in the backend. Of course it cannot do terminal output, but like I said, that should never have been made part of the file system in the first place.
Teddipetzi
Posts: 42
Joined: Sat Aug 17, 2019 2:32 am
Operating System: Windows 10/8.1/8/201x 64-bit
Graphics Processor: nVidia with Vulkan support

Re: Need some advice from C++ programming experts

Post by Teddipetzi »

Thanks, guys! That really confirms my suspicion of something not particularly good but too many people with an investment in the matter so they won't admit to the problem.

Return to “Off-Topic”