Recommended C++ Include Practice

In the following scenario, what would be the best approach to include the <string> header?


#include "extra.h"

int main() {
    func("A string");
    return 0;


#ifndef EXTRA_H
#define EXTRA_H

    #include <string>

    void func(std::string);



#include <iostream>
#include "extra.h"

void func(std::string str) {
    std::cout << str << 'n';


Your extra.h includes <string> because it uses it directly. There is no direct use of std::string in your main.cpp so it would be strange to include <string> there.

Further, including <string> in extra.cpp is totally unnecessary because the use of std::string is in a function signature which you know is declared in extra.h, plus the maintenance of the two extra files can reasonably be expected to be done as a single operation, so there’s no worry about extra.h suddenly not including <string> and then breaking extra.cpp, because they’d be maintained together.