Header files are not a way to only expose the interface. You give up a lot more in C++.
I've never had to deal with pesky header files until I started developing C and it immediately struct me as a royal pain in the ass. Even after couple of years of developing C/C++, I find the whole concept of header files archaic. Include preprocessor directive literally copy pastes stuff with no intelligence what-so-ever. The user is now burdened to ensure #includes are guarded to what I call a patchy half-baked hacked up solution - #IFDEF/#DEFINE/#ENDIF and #pragma in C++.
It should be handled automatically by the compiler/preprocessor or IDE and I believe it is now being addressed in the C++17/20 spec with the advent of "modules". This thing should have been written up way back in 1989.
Sorry, but it is not "completely false". Doing it properly requires a carefully designed interface to hide internal data structures, and splitting out the end user headers from the internal headers, but it works.
> I've never had to deal with pesky header files until I started developing C and it immediately struct me as a royal pain in the ass.
It's like saying, "I've never had to deal with pesky .py files until I started developing Python."
Out of curiosity, how? (If pointers or other mechanisms for memory indirection are allowed then it's pretty easy, so let's agree to ban those.)
Not a rhetorical question, despite the parenthetical.
Here's one popular way to do it in C++: https://en.cppreference.com/w/cpp/language/pimpl
I guess my tongue in cheek response to your parenthetical would be to ban the use of hidden internal data structures ;-)
Doing the same with objects require pointers to hide the details of any private members. A common pattern is called Pointer to IMPLemenation (PIMPL).
Not sure why we need to exclude pointers?
In c++14 this is very much simplified with the use of unique pointers.
std::unique_ptr<Foo> make_foo(...);
When the parent (er, great grandparent?) says their parent is wrong to deny that headers only reveal the interface, I think it's a little disingenuous to base that on an unrevealed assumption that PImpl is in play. PImpl is basically the the idiom that begot Java. One reason I might opt for C++ over Java, is a desire for finer control over the location of memory -- but it's important for me to know that to actually get that, I'll probably have to sacrifice information hiding. It's a trade-off. Yes, on some level it's better to have the option to make that trade-off, but the product here is "encapsulation or value semantics", not "encapsulation and value semantics".
Mind, I'm not a C++ developer. Maybe these days link-time heroic optimization makes the "right" decisions and collapses these kinds of indirections in all the sorts of situations you'd want it to. I write a lot more Java, and my understanding is that HotSpot gets up to a lot of heroics pertaining to this stuff these days -- I've noticed HotSpot will churn through a workload involving processing a collection of records far more quickly if you can arrange for it to stream through an array, even if you'd expect the records to be scattered randomly throughout memory.
Conceptually this should be possible with some preprocessor/header tinkering; something like:
private.h:
struct Bar {
double x;
double y;
};
#define BAR_DEFINED
public.h: #ifndef BAR_DEFINED
struct Bar {
char opaque[16];
};
#endif
struct Foo {
Bar bar;
};
consumer.cpp: #include "public.h"
private_implementation.cpp: #include "private.h"
#include "public.h"However I'd say it's an ugly hack which should only ever be used if you _really_ need the performance.
if you want to put the object on the stack, the compiler has to know the size of the object to reserve enough space on the stack. How can it know the size of the object if it does not have its full definition somewhere ?
Some languages have, like ADA I think, have first class support for runtime sized, stack allocated types, so it might work there.
Sometimes you need to. You cannot entirely stack allocate an object that uses PIMPL. Also you cannot allocate an array of such objects compactly in memory.
On the other hand, if you want to be able to evolve the class member variables but still maintain a stable ABI, you need to hide the memory layout, for example with PIMPL. But this is a C++ limitation. For example Objective-C* (and also soon Swift) allows modifying the class layout, adding properties etc, without changing the ABI.
https://en.wikipedia.org/wiki/Objective-C#Non-fragile_instan...
You actually can, if I'm not misunderstanding: http://www.gotw.ca/gotw/028.htm
so it's just syntax sugar for PIMPL.
In Objective-C 2 the object meta-data contains a table of instance variable offsets. The dynamic linker can modify this table at load time so you can freely add both instance variables and methods to new revisions of a class.
So what is the deal? Well, when the holder object itself is heap allocated, pimpl is inefficient because every access will require dereferencing two pointers. Also you cannot put protected or virtual members in the internal pimpl class (then there would be no point to have those in the first place).
That being said, it is not like Objective-C is some pinnacle of performance - you cannot allocate objects on the heap, and the compiler doesn't perform any devirtualization. So for performance critical code you have to drop down to C or...C++ :)
If you don't want to expose class internals, just use the PIMPL idiom. It's an extra indirection to protect your own abstraction, so naturally C++ decides to opt for performance by default.
sorry what ? most C libraries I know hide their implementations behind opaque types such as `typedef void* my_handle_t`.
struct my_type;
struct my_type *my_type_new(void);
This is more common than `typedef void*` in my experience, because it actually provides some minimal amount of type safety.You can also do this in C++ of course, but you can't use member functions this way. My real complaint here is that the Pimpl idiom in C++ is more cumbersome than a simple forward declaration and free functions, which is available in C.
Well, it's easy to reason about them. The thinking goes about the translation units.
The real problem is that the actual interface is not enforceable: the symbols in the binaries are just names.
100% agree. That said, that doesn't mean that they're the only way to do it, or even the best way.
But still, no IDE or tool I've seen does this better. A header file gives you a very nice overview, and it really isn't any hassle to speak of to keep them up to date.
It takes getting used to (as with everything when starting out with a new language), but before you know it you might even start to miss them in other languages.
Dear lord, all those broken builds I've seen over the years would love to talk to you about that.
They're also bad as an overview. They may have been good in the 1980s, but nowadays a proper documentation generator gives you better formatting, search features, and cross-references. Inline documentation is particularly obnoxious in header files: I like seeing function-level documentation alongside interface and implementation, but if I put the documentation in both the .cpp and the .h file it's duplicated in two places and easily gets out of date.
I have the feeling that the difference matters to almost nobody and should be avoidable by the few people it actually affects. I had more issues with colliding include guards than I had with symlinks and I still end up replacing CLASSNAME_H with PROJECT_CLASSNAME_H in our own headers every now and then since the autogenerated guards are too naive.
Include order is a valid complaint, the others truly are non issues.
I mean, we have other more pressing issues. Such as the fact that garbage collection is still a thing in 2018.
> ...but nowadays a proper documentation generator gives you better...
Examples of that?
> I like seeing function-level documentation alongside interface and implementation, but if I put the documentation in both the .cpp and the .h file it's duplicated in two places and easily gets out of date.
This is the only thing that I dislike about header files. And it is a constant reminder how bad IDEs are at handling this.
I hope he meant something else since he said "nowadays". Doxygen is ok if you haven't setup your build environment. But for when you actually are in the code (and somewhat familiar with it) I very much prefer just reading the header files than switching to a browser.
This happens because there is some redundancy between the header file and the source. You can often arrange your code so that there is no redundancy (or almost zero).
how so ? every IDE I've used automatically changes the header if you change the cpp and conversely
Modula-2 does it better. Each module split in two files - definition and implementation. Unlike C++ header files, def files are compiled rather that inlined, so they don't have a potential to produce different result for each source file that references them. The result is 100% reliable dependency graph across all source files, no need for a makefile, and blazingly fast compilation (each def file is only parsed and compiled once).
[1] https://discuss.ocaml.org/t/what-is-the-reason-of-separation...
Why aren't Delphi, Oberon, and Modula-3 immensely better? Each file is an independent module except the one with main. Then the compiler quickly figures out in one pass what all the function signatures are. The minor drawback with this is that the one-pass method requires you to pay attention to declaration order, though a two-pass method renders that approach irrelevant. The IDE has all the symbolic goodness you need, and no goddamn header files. Also it's still easier than dealing with managing the dependency graph yourself as is necessary with C file include order.
I say this as a guy who enjoys C and has done way more work in C than Delphi, but give credit where it's due.
"Uses BGI;" and I'm done :)
Thankfully there is Bitsavers.
"Circular Unit References" chapter on the Turbo Pascal 5.0 programmer's user guide.
https://archive.org/details/bitsavers_borlandturVersion5.0Us...
But they don't.
Also, a header file will hopefully be logically structured by a human. Something generated by the IDE will not.
You could even make them part of the build, if you want a continuous up to date version.
That makes a huge difference.
Headers (or at least 'separate specification of API') are/is great, and I miss it dearly in other languages.
Already in Turbo Pascal for MS-DOS there was a TPU dump tool, which would generate a "header file" from a TP module.
I suppose at the linker layer maybe but even that is just once globally for the program. Right? The OOP God's demand polymorphism!
If memory serves me, the C++ version is a class with all empty virtual functions.
Those 2 plus other crazy rules (like only one interface defined per file) mean that many projects will end up with just as many interface files as a c++ project would header files.