This question is a follow-up question of this one.
It is about the nvcc
compiler recognizing a static constexpr
class variable as undefined in device code, if the variable is odr-used. However, I could not find a reason, why it should not work.
The error message is:
error: identifier "Tester<int> ::ONE" is undefined in device code
compiled with
nvcc -std=c++11 -ccbin=/usr/bin/g++-4.9 -arch=sm_30 main.cu
The nvcc
compiler version is release 8.0, V8.0.26
.
An minimal example (a shortened version of the MWE in the previous question, concentrating on this particular issue) is given by
#include <iostream>
#include <cstdlib>
#ifdef __CUDACC__
#define HD __host__ __device__
#else
#define HD
#endif
HD void doSomething(const int& var ) {};
template<typename T> class Tester
{
public:
static constexpr int ONE = 1;
HD void test()
{
doSomething( ONE );
}
};
template<typename T> constexpr int Tester<T>::ONE;
int main()
{
using t = int;
Tester<t> tester;
tester.test();
return EXIT_SUCCESS;
}
The question is not about fixing this particular code (which would be done by passing var by value instead of const reference - at least the compiler does not complain any more, although it is an odr-use, isn't it?).
The question is, whether this is a bug of the nvcc
compiler or if there is some good reason, why this does not work (I did not find any hints on that on the NVIDIA pages...).