考虑以下程序:
class A {
__host__ __device__ void foo();
};
class B : A {
__host__ void foo();
};
int main()
{
A a; (void) a;
B b; (void) b;
}
此compiles(GodBolt)与nvcc 10。
但是,在更复杂的程序中,有时会出现以下错误(为了可读性而换行):
whatever.hpp(88): error: execution space mismatch: overridden entity (function
"C::foo") is a __host__ __device__ function, but overriding entity (function "D::foo")
is a __host__ function
所以,nvcc告诉我,我[[not应该在覆盖方法时删除执行空间。我不是在问自己的代码(在这里没有引用),而是在问原理:
__host__ __device__
个函数(我认为是合理的)覆盖__host__
个函数是可以接受的,那么nvcc怎么会出现这种错误?C::foo()
必须为marked virtual
的情况下。实际上,如果我们在示例程序中将foo()
标记为虚拟:
class A {
virtual __host__ __device__ void foo();
};
class B : A {
__host__ void foo(); // can say "override" here; but it doesn't matter
};
int main()
{
A a; (void) a;
B b; (void) b;
}
这将是fail to compile:
<source>(6): error: member function declared with "override" does not override a base class member
此限制有意义吗?可以想象一种解释,其中基类方法将应用于__device__
端调用,而子类方法将应用于__host__
端调用。但这也有点尴尬-当通过基类ptr对对象进行操作时,我们需要调用something。