3 ms·
As a non c++ programmer, what I don't understand is why do people need to write header files by hand. Surely it should be auto-generated from the source code?
by cm2187 8y ago
As a non c++ programmer, what I don't understand is why do people need to write header files by hand. Surely it should be auto-generated from the source code?
- berti 8y agoHeaders are used control visibility of declarations (note though that visibility of symbols themselves is controlled by other means), and typically contain more than just function declarations (structs, enums, etc.). There are basically two common patterns to split public and private declarations: - public header for consumers of the interface, private header for internal use only - public header for consumers, private declarations directly in the .cpp/.c file
- MaxBarraclough 8y agoHeader-generator tools are out there, but I'm not sure how usable they are. It's rarely done though. It's a bit of a pain having to 'write everything twice', but there's more to header files than re-declaring what you've done in your source files. As berti said: enums, constants, macros, simple struct type declarations, typedefs, and templates, are examples of hand-written code that might belong entirely in the header file. It would be possible to keep these constructs in a hand-written header file while auto-generating the rest, but C++ developers aren't convinced this is worth doing.
- bluGill 8y agoRemember you are writing your comment in 2018, what was feasible in 1975 was very different. Programmers back then were smart enough to pull the job off, but the computers they had were limited such that it wasn't worth it. First because the computers were limited programs had to be smaller so the benefit wasn't as great. Second computers were slower so it was worth spending extra human effort once to save a lot of computer effort. C++ has been working on modules to fix this for years now. It turns out to be harder than you would think. Everyone agrees with the basic problems statement, but there is disagreement on the details. All sides have good points in favor of their approach, and there are some places where you have both as they are incompatible. Progress is being made (and a lot of the incompatibilities turned out to be solvable but it took years of thinking to come up with how)
- kccqzy 8y agoA C++ header file is much more than just serving as an interface to an implementation. Sure it can be used in that way, but it doesn't have to. Have you checked out the standard library's header files? If you do, you'll see they are full of templates, and constexpr functions. In some sense, the header file is where most of the code is. Have you also checked out the concept of a single-header dependency? Due to the myriad different ways to manage dependencies, those dependencies that consist of just a header file is extremely convenient to have. This is the logical conclusion when you can put increasingly complicated things in header files.