NVCC compilation error with template parameter as a friend within a namespace

nvcc --version
Cuda compilation tools, release 9.1, V9.1.85

namespace NS {
template <class C>
class Helper
{
    friend C;
};

struct S {};
} //NS

int main()
{
    NS::Helper<NS::S> h;
    return 0;
}

When compiling with: nvcc --std=c++11 I get the following error:

test.cu:5:11: error: ‘C’ in namespace ‘::’ does not name a type

Without the namespace the code compiles.

Based on the output of nvcc -v --keep it appears the friend class is tagged in the global scope, which it does not belong to:

# 1 "test.cu"
namespace NS { 
# 2
template< class C> 
# 3
class Helper { 
# 5
friend  <b>::C</b>; 
# 6
}; 
# 8
struct S { }; 
# 9
}

This seems like an erroneous translation by nvcc.

I think this issue is known to NVIDIA and has been reported elsewhere. If I am correct, a fix is in the works and should be available in a future CUDA release.

You can use a dummy typedef as workaround:

namespace NS {
	template <class C>
	class Helper
	{
		template<typename DummyType> struct dummy{typedef DummyType type;};
		
		friend typename dummy<C>::type;
	};

	struct S {};
} //NS

int main()
{
    NS::Helper<NS::S> h;
    return 0;
}