为什么类型在向量标头之后包含时会转义命名空间?

问题描述 投票:0回答:1

如果我用 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());
  }
}

我收到以下错误:

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 */
                                ^

但是,如果我不在 ffi 命名空间之前包含向量标头,则编译可以正常工作。

本质上,为什么向量头阻止类型被包含到 ffi 命名空间中?

c++ namespaces include
1个回答
0
投票

<sys/types.h>
已通过
#include <system_error>
#include <vector>
隐式包含在内。命名空间中的第二个
#include <sys/types.h>
<sys/types.h>
中的标头防护阻止。

© www.soinside.com 2019 - 2024. All rights reserved.