Search code examples
c++namespacesinclude

Why are types escaping namespace when included after vector header?


If I compile this minimized example with clang++:

#include <system_error>
#include <vector>

namespace MyNamespace {
  namespace ffi {
#include <sys/types.h>
  }

  void example() {
    const ffi::errno_t savedErrno = errno;
    ffi::uint uskip = static_cast<ffi::uint>(5);
    throw std::system_error(savedErrno, std::generic_category());
  }
}

I get the following errors:

clang++ -pedantic-errors -Weverything -Wno-c++98-compat -Wno-pre-c++20-compat-pedantic -Wno-poison-system-directories --std=c++20 -O3 -Iinclude -I/usr/local/include -MMD -MP -c -fPIC src/test.cpp -o obj/test.o
src/test.cpp:10:11: error: no type named 'errno_t' in namespace 'MyNamespace::ffi'; did you mean simply 'errno_t'?
    const ffi::errno_t savedErrno = errno;
          ^~~~~~~~~~~~
          errno_t
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/_types/_errno_t.h:30:32: note: 'errno_t' declared here
typedef int                    errno_t;
                               ^
src/test.cpp:11:5: error: no type named 'uint' in namespace 'MyNamespace::ffi'; did you mean simply 'uint'?
    ffi::uint uskip = static_cast<ffi::uint>(5);
    ^~~~~~~~~
    uint
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/types.h:93:33: note: 'uint' declared here
typedef unsigned int            uint;           /* Sys V compatibility */
                                ^
src/test.cpp:11:35: error: no type named 'uint' in namespace 'MyNamespace::ffi'; did you mean simply 'uint'?
    ffi::uint uskip = static_cast<ffi::uint>(5);
                                  ^~~~~~~~~
                                  uint
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/types.h:93:33: note: 'uint' declared here
typedef unsigned int            uint;           /* Sys V compatibility */
                                ^

However, if I don't include the <vector> header before the ffi namespace, then compilation works.

Essentially, why is the <vector> header preventing the types from being included into the ffi namespace?


Solution

  • <sys/types.h> is already included implicitly through #include <system_error> and #include <vector>. The second #include <sys/types.h> in the namespace MyNamespace::ffi is prevented by the header guards in <sys/types.h>.