Today, I complete the rules of the C++ core guidelines to source files. They are about header files and namespaces.
Let's see how I can make a story out of the remaining rules because they have not much content. Anyway, here are they:
#include
.h
SF.11: Header files should be self-contained
namespace
The first rule is already best practice.
When you put an include guard around your header file, it is only be included once. Here is a small example from the guidelines.
// file foobar.h:
#ifndef LIBRARY_FOOBAR_H
#define LIBRARY_FOOBAR_H
// ... declarations ...
#endif // LIBRARY_FOOBAR_H
There are two points to keep in mind.
// file foobar.h:
#pragma once
// ... declarations ...
For example, GCC supports the preprocessor directive since 3.4. Read the details about the #pragma once support in the Wikipedia article.
Okay, the next rule is tricky.
First of all, what is a cyclic dependency of source files? Imagine, I have the following source files.
#ifndef LIBRARY_A_H
#define LIBRARY_A_H
#include "b.h"
class A {
B b;
};
#endif // LIBRARY_A_H
#ifndef LIBRARY_B_H
#define LIBRARY_B_H
#include "a.h"
class B {
A a;
};
#endif // LIBRARY_B_H
#include "a.h"
int main() {
A a;
}
When I try to compile the program, it will fail:
The issue is that there is a circular dependency between the headers a.h and b.h. The problem manifests itself when a is created in the main program. To create an A, the compiler must figure out the size of B. To create a B, the compiler must figure out the size of A. This is not possible if a or b are objects. It is only possible if a or b are pointers or references.
The easy fix is, therefore, to forward declare A in b.h or B in a.h. Depending on your platform, the size is now 32 or 64 bits. Here is the modified header of a.h.
#ifndef LIBRARY_A_H
#define LIBRARY_A_H
class B;
class A {
B* b;
B& b2 = *b;
};
#endif // LIBRARY_A_H
By the way, the standard library header <iosfwd> holds forward declarations of the input/output library.
The next issue happens to me a few times.
For example, the following program will compile with GCC 5.4 but will break with the Microsoft compiler 19.00.23506.
#include <iostream>
int main(){
std::string s = "Hello World";
std::cout << s;
}
I forgot to include a necessary header <string>. GCC 5.4 includes <string> with the header <iostream>. This does not hold true for the Microsoft compiler. The error message of the Microsoft compiler is quite verbose.
The next rule is short but important.
A self-contained header file can be included top-most in a translation unit. This means it does not depend on other headers that are included before. If you don't follow this rule, a user of your header may be surprised by difficult to understand error messages. Sometimes the header seems to work; sometimes not. It just depends on which header was included before.
The last three rules to source files are about namespaces. It starts with a no-brainer.
Obviously, we have namespaces in the C++ standard to express logical structure. Examples? Here are a few:
std
std::chrono
std::literalsstd::literals::chrono_literals
std::filesystemstd::placeholders
std::view // C++20
The next two rules are about unnamed (anonymous) namespaces.
An unnamed namespace has internal linkage. This means, that names inside the unnamed namespace can only be referred from within the current translation unit and are not exported (SF22). The same holds for namespaces, which are declared in the unnamed namespace. Okay, what does that mean?
namespace {
int i; // defines ::(unique_name)::i
}
void inc() {
i++; // increments ::(unique_name)::i
}
When you refer to i from within the translation unit, you do it by a unique_name without a name clash. For example, you can define a free addition function add inside the unnamed namespace, and the linker would not complain because you broke the one definition rule.
And here is the problem, when you use an unnamed namespace in the header (SF21). Each translation unit will define its unique instance of the unnamed namespace. Unnamed namespaces in headers have a few consequences:
The usage of an unnamed namespace is similar to the static keyword used in C.
namespace { int i1; }
static int i2;
The C++ core guidelines mentioned a few times modules in the rules to source files, but they didn't write about the big C++20 feature. Let me fill the gap 174
Yesterday 5796
Week 34605
Month 211926
All 9685558
Currently are 134 guests and no members online
Kubik-Rubik Joomla! Extensions
Read more...
Read more...