c++fstreamclang++setw

Using std::setw() without <iomanip> header


How is it possible for this code to compile even though I didn't include <iomanip> ?

#include <iostream>
#include <fstream>

int main()
{
    std::cout << std::setw(5) << "test" << std::endl;
    return 0;
}

Compiles with:

clang++ test.cpp

But without <fstream> it throws the error:

test.cpp:5:20: error: no member named 'setw' in namespace 'std'
        std::cout << std::setw(5) << "test" << std::endl;
                     ~~~~~^
1 error generated.

On my friends Mac it throws error in both situations.


Solution

  • The headers include themselves internally, depending on the standard library implementation.

    The standard doesn't guarantee that a symbol is undefined unless you include a certain file - instead it guarantees that a symbol will be defined if you do include it.

    In this case, the fstream header includes code internally that happens to also have the definition of std::setw.

    The code compiling on your compiler is a particularity of the implementation of your standard library.

    In our current project, missing headers are one of the common causes of failed builds (build works on Windows but not on Mac, or the other way around).

    As a rule, include the specified headers for everything you require in your code, even if the code compiles OK without the include.