Я внедряю полиморфизм на графическом процессоре с помощью std :: cuda :: variant , пока он работает с Circle, Square, Shape :
struct Circle;
struct Square;
using Shape = cuda::std::variant;
auto shape = gpu_allocate(1);
*shape = Circle(); //initialize Shape with Circle
< /code>
Но когда дело доходит до множественного наследования, я не смог найти способ инициализации переменной: < /p>
struct Color {
enum Type { red, blue };
Type type;
};
struct ColoredShape : Color, cuda::std::variant {};
auto colored_shape = gpu_allocate(1);
colored_shape->type = Color::red;
// *colored_shape = Circle(); // error
Итак, мой вопрос: как инициализировать цветную форму переменную с помощью круга ?
Полный код:
#include
struct Circle {
double radius = 0.9;
void init() { radius = 0.3; }
};
struct Square {
double x = -0.1;
double y = -0.3;
void init() {
x = 0.1;
y = 0.3;
}
};
struct Color {
enum Type { red, blue };
Type type;
};
using Shape = cuda::std::variant;
struct ColoredShape : Color, cuda::std::variant {};
template T *gpu_allocate(const size_t num = 1) {
T *data;
const auto size = sizeof(T) * num;
cudaMallocManaged(&data, size);
return data;
}
int main() {
auto shape = gpu_allocate(1);
*shape = Circle();
auto colored_shape = gpu_allocate(1);
colored_shape->type = Color::red;
// *colored_shape = Circle(); // error
return 0;
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... stdvariant