范围运算符需要查找 std 命名空间而不是提升

Scope operator needed to find std namespace instead of boost

本文关键字:命名空间 std 运算符 查找 范围      更新时间:2023-10-16

我正在努力移植提升库的一部分,以便在 cuda/nvcc 下编译为设备函数。这涉及将推力库用于某些事情,例如迭代器、数组等。我发现的一个问题是推力库中的编译错误,例如:

    C:Program FilesNVIDIA GPU Computing ToolkitCUDAv9.1includethrust/iterator/iterator_traits.h(66): error : namespace "boost::std" has no member "ptrdiff_t"

这是由推力中的线触发的:

   typedef std::ptrdiff_t difference_type;

我可以通过在 std lib 调用之前添加范围运算符 :: 来解决此问题,例如:

   typedef ::std::ptrdiff_t difference_type;

但显然不能修改推力。

有谁知道为什么我会遇到这个问题? 即为什么推力标头iterator_traits.h在命名空间boost::std而不是std中搜索std::p trdiff_t?在我包含推力集管之前,有没有办法扭转这种情况?

由于移植像 boost 这样的大型库的性质,在这里提供一个最小的工作示例并不容易。

谢谢!

我只能在这里猜测,但我最好的猜测是,由于某种原因,缺少一个右大括号,用于在打开 std 命名空间之前关闭 boost 命名空间的某个位置,可能是通过包含标准库标头。然后,这会导致命名空间boost::std存在,以便编译器在该子命名空间中查找std::ptrdiff_t,因为boost命名空间当前处于打开状态。

例如,使用 gcc 编译以下源文件

#include <cstddef>
namespace foo {
// this creates a namespace ::foo::std
#include <typeinfo>
}
namespace foo {
    using difference_type = std::ptrdiff_t;
}

还打印

prog.cc:11:34: error: 'ptrdiff_t' in namespace 'foo::std' does not name a type
   11 |     using difference_type = std::ptrdiff_t;
      |                                  ^~~~~~~~~

正如你也看到的 这里.