Search code examples
c++boostscopecudathrust

Scope operator needed to find std namespace instead of boost


I am working on porting parts of the boost library to compile under cuda / nvcc as device functions. This involves using the thrust library for some things like iterators, arrays, etc. One issue I am finding is a compile error in the thrust library such as:

    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\include\thrust/iterator/iterator_traits.h(66): error : namespace "boost::std" has no member "ptrdiff_t"

which is triggered by the line in thrust:

   typedef std::ptrdiff_t difference_type;

I can fix this by adding the scope operator :: ahead of the std lib call like:

   typedef ::std::ptrdiff_t difference_type;

but clearly it is not okay to modify thrust.

Does anyone know why I am getting this issue? i.e. why does the thrust header iterator_traits.h search for std::ptrdiff_t inside the namespace boost::std rather than std? Is there a way to reverse this before I include the thrust header?

It is not easy to provide a minimal working example here due to the nature of porting a large library like boost.

Thanks!


Solution

  • I can only guess here, but my best guess is that for some reason there is a missing closing curly brace to close the boost namespace somewhere before the std namespace is opened, probably by including a standard library header. This then causes the namespace boost::std to exist so the compiler looks in that sub-namespace for std::ptrdiff_t as the boost namespace is currently open.

    E.g. compiling the following source file with gcc

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

    also prints

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

    as you can also see here.