Consider a foo.cpp
file with the following content
#include "foo.hpp"
int foo() {
return 7;
}
and its associated header
#pragma once
int foo();
The latter is obviously needed to make aware the following main
function of the existence of foo
:
#include <iostream>
#include "foo.hpp" // to make the name `foo` available
int main() {
std::cout << foo() << std::endl;
}
However, the #include "foo.hpp"
seems to be redundant. Is there any reason I should keep it?
I've seen this practice in the codebase where I work on, but I guess there's many examples available in open source. For instance, as an example picked at random, look at src/builtin_builtin.h
and src/builtin_bultin.cpp
from the fish-shell
code base: the former, beside the include guard, has
just
- one
#include
, - two class declarations,
- and a function declaration.
One could put 2 in a fwd header, include it in the cpp file together with 1, and then the cpp file wouldn't need anymore to include its own header.