17
votes
Accepted
Is inlining almost all of my C++ application's methods a good or bad idea?
When you're writing C++, don't write it as if it were Python or D or Java. Those are fundamentally different languages with different idioms.
When we are talking about inline functions in C++, we ...
15
votes
Is there any reason *not* to forward declare all forward declarable function parameter/return types?
Yes, at least one reason exists.
The Google Style Guide acknowledges that forward declarations can improve compile time, but still advises developers to prefer #include declarations when possible (...
9
votes
Should I specify my header include path in the source code, or as a project option?
In my current workplace, various projects are using boost versions ranging from 1.60 to 1.66, and at appropriate times we move those forward. It's much simpler to repoint the project includes to a ...
9
votes
Accepted
Include own header file first or last? Any technical reasons?
#include directives are processed in the order in which they are encountered in the sources. Conceptually, when a #include is encountered, that directive is replaced with the content of the referenced ...
8
votes
Accepted
Is it possible to create a C header file from a dynamic library?
In general, it won't be possible (at least not with ELF files on Linux). Because type and signature information is not kept (e.g. in ELF symbol files). But C++ compilers are doing name mangling to ...
8
votes
Accepted
Detecting header inclusion chains and dependencies in C++
This is a common but unsolvable problem with C++. A header provides some declarations. How the header provides these declarations is an implementation detail, and it is completely valid if the header ...
5
votes
Is inlining almost all of my C++ application's methods a good or bad idea?
If by inlining you mean use the inline function, you don't need to:
mainstream compilers now perform global optimisation and will do it automatically for you if it has a real performance advantage.
...
5
votes
When to ever use separate implementation files (cpp files) in modern c++?
To answer your literal question: technically, there are two reasons for using cpp files (and not just header files):
Decreased compiled times (which you cannot disregard when you are ever going to ...
5
votes
Anti-pattern? Double header and exposed implementation detail
What “multi-threading” are you using that makes malloc inefficient? Typically allocation and deallocation on the same thread has zero overhead and is a lot easier to handle than stack allocation.
Just ...
4
votes
Passing set of flags from browser to backend by message header or body?
We generally try to keep data together, so any business information that the main process logic is dependent on should be in the payload. We generally expect that the main body content will be passed ...
3
votes
Accepted
Documenting classes with doxygen (I want a birds eye view of the code but also want documentation)
Doxygen supports various ways to place your documentation comments, not just the big block before a declaration or definition.
Some ways to have documentation comments and keep your declarations ...
3
votes
Accepted
MVP where to set Content-type header?
As talking about content-type headers only makes sense in the context of a remotely rendered view, as it typical for web-applications, I will assume that is the case here as well.
The content-type ...
3
votes
Should I put request context in path or in headers?
URLs should identify resources. So this depends on the semantics of your requests.
I would go neither way, but one in between. Without knowing you exact use cases, I'd argue that the backend-id and ...
3
votes
Is it possible to create a C header file from a dynamic library?
No, this is not generally possible. Libraries are self-describing in that they list all available symbols (functions and variables with external linkage). But they do not contain sufficient ...
3
votes
Is it possible to create a C header file from a dynamic library?
It depends on the compilation system, and especially if only C or a C & C++ compiler, as these are aspects of the development system that are not standardized behaviors.
Some systems will define ...
3
votes
Why do we need to put private members in headers?
The primary reason this is needed is that any code that uses a class needs to know about private class members in order to generate code that can handle it.
Consider the following class:
//foo.h
...
3
votes
Why can you have the method definition inside the header file in C++ when in C you cannot?
C++ standard quotes
The C++17 N4659 standard draft 10.1.6
"The inline specifier" says that methods are implicitly inline:
4 A function defined within a class definition is an inline function.
and ...
2
votes
Is it good practice to rely on headers being included transitively?
There can be another case:
You have A.h, B.h and your C.cpp, B.h includes A.h
so in C.cpp, you can write
#include "B.h"
#include "A.h" // < this can be optional as B.h already has all the stuff ...
2
votes
Why do we have to include multiple header files for a single library (the C standard library)?
New Identifiers
C, over the years, has added various new Standard Library functions, types, defines, etc.
It is possible that older code may have used prior some of these new identifiers.
A classic ...
2
votes
Detecting header inclusion chains and dependencies in C++
A rule that you should follow in languages like C, C++, Objective-C is that every single header file compiles on its own. This is achieved here (so someone including bottom.h doesn’t need to remember ...
2
votes
Accepted
What is the added value of using cache control header on top of CDN for a web application?
Cache control headers put the client in control of caching. This is desirable when the data is static for assets like images, JavaScript, style sheets, etc. Client side caching was originally built ...
2
votes
Accepted
How to separate public and private headers when internal data structures are private?
The easiest way is to use the PIMPL idiom, also colloquially called “compilation firewall”.
This technique does not need conditional compilation to define the structure differently: in your public ...
2
votes
Accepted
Recommended way of hiding implementation details?
Trying to extract implementation details from your main cpp to separate files might be counter productive since it would require to give those details more visibility than they deserve. Of course, ...
2
votes
Anti-pattern? Double header and exposed implementation detail
Ok you feel a need to expose a compile-time constant-valued size expression for the opaque structure.
Assuming that this structure's implementation is not composed entirely of chars the size of the ...
1
vote
If you could define function by prepending `inline` in header file, why people recommend declare in header then define in a seperate .cpp file?
Then why people bother to use the method "Declare function in header first, then define it in a seperate .cpp file" ...
It's partly about Separation of Concerns and partly about Timing:
...
1
vote
Common header file for C++ and JavaScipt, redux
There are a few considerations to take into account.
Even unused constants will take up memory and processing time on the client's browser. How much of an issue this might be depends on how many of ...
1
vote
Accepted
Getting file format by checking file header
Use memcmp to compare the PNG header against the first bytes of the file. Other than that you can't avoid opening and reading the file.
Maybe you should fseek back to the start, to reset the file ...
1
vote
Detecting header inclusion chains and dependencies in C++
Starting in Visual Studio 17.8 Preview 1, there's an #include cleanup feature that does what you are looking for. It finds #includes that you are using but aren't explicitly #including and adds them ...
1
vote
Detecting header inclusion chains and dependencies in C++
Use unit tests.
You arrange your unit test code so that each unit test/suite runs in a separate translation unit. This will mean that each header gets pulled and trialled in one or more alternative ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
headers × 77c++ × 34
c × 21
include × 9
coding-style × 6
http × 6
php × 5
rest × 4
api-design × 4
implementations × 4
definition × 4
programming-practices × 3
api × 3
libraries × 3
http-request × 3
design × 2
javascript × 2
web-development × 2
programming-languages × 2
documentation × 2
coding-standards × 2
authentication × 2
functions × 2
source-code × 2
encapsulation × 2