C++ Core Guidelines: The Remaining Rules to Source Files

Today, I complete the rules of the C++ core guidelines to source files. They are about header files and namespaces.


vga 160266 1280

Let's see how I can make a story out of the remaining rules because they have not much content. Anyway, here are they:

The first rule is already best practice.

SF.8: Use #include guards for all .h files

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:
// ... declarations ...

There are two points to keep in mind.

  1. Give your guard a unique name. If you use a guard name more than once, it may exclude the inclusion of a header file.
  2. #pragma once is non-standard but widely supported preprocessor directive. This means, the following variation of the header foobar.h is not portable.
// 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.

SF.9: Avoid cyclic dependencies among source files

First of all, what is a cyclic dependency of source files? Imagine, I have the following source files.

  •  a.h
#ifndef LIBRARY_A_H
#define LIBRARY_A_H
#include "b.h"

class A {
  B b;

#endif // LIBRARY_A_H
  • b.h
#ifndef LIBRARY_B_H
#define LIBRARY_B_H
#include "a.h"

class B {
  A a;

#endif // LIBRARY_B_H
  • main.cpp
#include "a.h"

int main() {
  A a;

When I try to compile the program, it will fail:dependency

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.

SF.10: Avoid dependencies on implicitly #included names

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.

SF.11: Header files should be self-contained

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.

SF.20: Use namespaces to express logical structure

Obviously, we have namespaces in the C++ standard to express logical structure. Examples? Here are a few:

std::literals::chrono_literals std::filesystem
std::placeholders std::view // C++20


The next two rules are about unnamed (anonymous) namespaces.

SF.21: Don’t use an unnamed (anonymous) namespace in a header and SF.22: Use an unnamed (anonymous) namespace for all internal/nonexported entities

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 resulting executable will bloat.
  • Any declaration in an unnamed namespace refers to a different entity in each translation unit. This may not be the expected behaviour.

The usage of an unnamed namespace is similar to the static keyword used in C.

namespace { int i1; }
static int i2;

What's next?

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 in my next post.



Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner,  Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, and Tornike Porchxidze.


Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, and Richard Sargeant.

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small



I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable (Online)



Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.


Contact Me

Modernes C++,





My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code


Today 3401

Yesterday 5600

Week 47513

Month 73139

All 6094203

Currently are 202 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments